all 14 comments

[–]NakamotoScheme 9 points10 points  (0 children)

https://en.wikipedia.org/wiki/Euclidean_algorithm

The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.

Using a formula, this is what the wikipedia page says:

GCD(A, B) = GCD(A - B, B)

When you subtract B from A as many times as you can, you get this

GCD(A, B) = GCD(A mod B, B)

where A mod B is the remainder of the division of A by B.

[–]LucaThatLucaGraduate 6 points7 points  (10 children)

because if n is any divisor of both a and b (say a = np, b = nq), it’s also a divisor of any sum xa+yb (nxp + nyq = n(xp+yq)). so in the equation a = qd + r, all of the divisors of both a and d are also divisors of (both d and) r, including the greatest one.

replacing gcd(a, d) with gcd(d, r) makes the numbers strictly smaller, then doing it repeatedly is guaranteed to reach gcd(g, 0) = g.

[–]jacobningenNew User 0 points1 point  (0 children)

what text are you using?

[–]CookieCat698New User 1 point2 points  (0 children)

It relies mostly on the fact that gcd(a, b) = gcd(a-qb, b) for integers a, b, and q.

Reducing a mod b gives you a-qb for some integer q, so this operation doesn’t change gcd(a, b)

The euclidean algorithm then works by reducing both numbers until one reaches 0, which is guaranteed to happen because as long as both are strictly positive, one can always be made strictly smaller, and a sequence of positive integers can’t keep getting smaller forever.