Greatest Common Divisor
beginnerUp next
Up next
Output will appear here...
Complete the function gcd(a, b) so that it returns the greatest common divisor of two non-negative integers: the largest number that divides both of them evenly.
For 12 and 18, a correct solution prints 6. If one of the numbers is 0, the answer is the other number.
Tip
You can try every number from the smaller value down to 1 and return the first one that divides both. For a faster approach, look up Euclid's algorithm: repeatedly replace a, b with b, a % b until b is 0.
No comments yet. Start the discussion.