Calculate the Greatest Common Divisor (GCD) of two positive integers using the Euclidean algorithm. The GCD is the largest number that divides both numbers without leaving a remainder. Return null if either number is null or not positive.

Examples:

Input: a = 48, b = 18 Output: 6 Explanation: 6 is the largest number that divides both 48 and 18 Input: a = 17, b = 5 Output: 1 Explanation: Coprime numbers share no common divisors except 1 Input: a = 100, b = 25 Output: 25 Explanation: 25 divides evenly into 100 Input: a = 0, b = 5 Output: null Explanation: Zero is not positive, return null Input: a = null, b = 5 Output: null Explanation: Null input, return null
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay October 2026 (Oct 4–15)Sponsored by FLXBL

Contest runs October 4 - 15. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 12 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In