Dynamic Programming21 sections · 916 units
Open in Course

GCD - Walkthrough

Tracing Euclid's algorithm

Let me trace gcd(4848, 1818):

Call 11: gcd(4848, 1818) \rightarrow 48mod18=1248 \mod 18 = 12, so call gcd(1818, 1212)

Call 22: gcd(1818, 1212) \rightarrow 18mod12=618 \mod 12 = 6, so call gcd(1212, 66)

Call 33: gcd(1212, 66) \rightarrow 12mod6=012 \mod 6 = 0, so call gcd(66, 00)

Call 44: gcd(66, 00) hits base case (b=0b = 0), returns 66 The GCD of 4848 and 1818 is 66. Notice how quickly the numbers shrink. This is why Euclid's algorithm is so efficient.

unnamed (13) (1).jpg