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
- ainteger
- the first positive integer
- binteger
- the second positive integer
- Returnsinteger
- the largest integer that divides both a and b
Constraints
1 ≤ a ≤ 1091 ≤ b ≤ 109
Examples
- Input
- a = 12b = 18
- Output
- 6
- Explanation
- The divisors of
12are 1, 2, 3, 4, 6 and 12; the divisors of18are 1, 2, 3, 6, 9 and 18. The largest one on both lists is6.
- Input
- a = 17b = 5
- Output
- 1
- Explanation
17and5are both prime and different, so the only divisor they share is1.
- Input
- a = 42b = 42
- Output
- 42
- Explanation
- A number divides itself, and nothing larger than
42can divide42, so the greatest common divisor of42and42is42.
Follow-up
Can you extend Euclid's algorithm to also return integers x and y with a × x + b × y = gcd(a, b)?
Hints
Open them one at a time. Each one gives away a little more.
A common divisor of
aandbcan never be larger than the smaller of the two. How many candidates would you have to try for two numbers near10^9?Any number that divides both
aandbalso dividesa % b. Sogcd(a, b)equalsgcd(b, a % b), and the second pair is smaller.Keep replacing the pair
(a, b)with(b, a % b). When the second number reaches0, the first one is the answer.
Solution
Trying candidates one by one works on small numbers, but two numbers near 10^9 that share no factor force a billion tries. Euclid's rule gcd(a, b) = gcd(b, a % b) shrinks the pair so fast that no pair up to 10^9 needs more than 43 steps.
Count down from the smaller number
Correct, but does not finish on the largest tests
Intuition
No common divisor can exceed the smaller number. Start d at min(a, b) and move it down by one until it divides both. Since you go from the top, the first hit is the greatest, and the loop always stops by 1. For 12 and 18 you try 12 down to 7 and stop at 6.
The cost is one try per candidate. For the primes 999999937 and 999999929 the answer is 1, so the loop runs almost 10^9 times.
Algorithm
- Set
dto the smaller ofaandb. - While
a % dorb % dis not0, subtract 1 fromd. - Return
d.
def gcd(a, b):
d = min(a, b)
while a % d != 0 or b % d != 0:
d -= 1
return dEuclid's algorithm
Intuition
Write a = q × b + r with r = a % b. A number that divides a and b also divides r = a - q × b, and a number that divides b and r also divides a = q × b + r. So (a, b) and (b, r) have the same common divisors, and the same greatest one.
Repeat until b is 0. Every number divides 0, so gcd(a, 0) = a. For 12 and 18 the pair goes (12, 18), (18, 12), (12, 6), (6, 0), and the answer is 6. When a is smaller, the first step swaps the pair, so you never sort.
Every two steps at least halve the larger number, so the loop runs O(log(min(a, b))) times.
Algorithm
- While
bis not0, computer = a % b. - Set
a = bandb = r. - When
breaches0, returna.
def gcd(a, b):
# gcd(a, b) == gcd(b, a % b), and gcd(a, 0) == a.
while b != 0:
a, b = b, a % b
return a
Pitfalls and edge cases
- Updating in the wrong order.
a = bfollowed byb = a % bcomputesb % b, always0. Save the remainder first, or assign both at once. - Returning
bwhen the loop ends. At that pointbis0. - Stopping the countdown at
2. Coprime pairs such as17and5then find no answer. - Repeated subtraction instead of the remainder.
gcd(10^9, 1)then takes a billion subtractions; one%does them all.
Frequently asked questions3
What is the time complexity of Euclid's algorithm?
O(log(min(a, b))) steps and O(1) space. The slowest inputs are consecutive Fibonacci numbers: 701408733 and 433494437 take 42 steps, or 43 with the smaller first, the most for any pair up to 10^9.
What is the difference between GCD and LCM?
The greatest common divisor is the largest number that divides both inputs; the least common multiple is the smallest number both inputs divide. They are linked by gcd(a, b) × lcm(a, b) = a × b, so the lcm is a / gcd(a, b) × b.
What is the gcd of two coprime numbers?
It is 1: two numbers are coprime exactly when they share no prime factor. Two different primes are always coprime, and so are two consecutive integers such as 8 and 9.
Similar problems
Problems that use the same ideas. Solving two or three of them is what makes a pattern stick.
def gcd(a, b):
# Write code hereTest #1
Test #2
Test #3
Arguments
a = 12 b = 18
Expected Output
6