Menu
CoddyTech

Greatest Common Divisor

You get two positive integers a and b. Return their greatest common divisor: the largest integer that divides both of them with no remainder.

For example, the numbers that divide both 8 and 12 are 1, 2 and 4, so the answer is 4.

Function

gcd(a: integer, b: integer) → integer
ainteger
the first positive integer
binteger
the second positive integer
Returnsinteger
the largest integer that divides both a and b

Constraints

  • 1 ≤ a ≤ 109
  • 1 ≤ b ≤ 109

Examples

Input
a = 12b = 18
Output
6
Explanation
The divisors of 12 are 1, 2, 3, 4, 6 and 12; the divisors of 18 are 1, 2, 3, 6, 9 and 18. The largest one on both lists is 6.

lock icon+14 hidden tests on Submit

challenge icon

Follow-up

Can you extend Euclid's algorithm to also return integers x and y with a × x + b × y = gcd(a, b)?

Reset code
def gcd(a, b):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

a = 12
b = 18

Expected

6