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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Jan 2026

Contest runs January 1 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.