All problems
Greatest Common Divisor
Problem
Compute the greatest common divisor of two non-negative integers a and b using the Euclidean algorithm.
Repeatedly replace the pair (a, b) with (b, a mod b) until b becomes 0, then return a. By convention gcd(0, 0) is 0.
Hint
gcd(a, b) equals gcd(b, a mod b); the process ends when the second value is 0.
Test cases
case 1
in: [48,18]
expected: 6
case 2
in: [0,5]
expected: 5
case 3
in: [0,0]
expected: 0
case 4
in: [13,7]
expected: 1
case 5
in: [270,192]
expected: 6
Starter codePython
def gcd(a, b):
# Your code here
pass5 test cases · run your code against them in the editor