Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 52s1 days 16 hours 1 minutes 52 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 52s1 days 16 hours 1 minutes 52 seconds.

Sign in to watch this lesson
Algorithms - Bits and Number Theory · 4:18
The greatest common divisor through a picture: a 21 x 13 rectangle tiled greedily by squares 13, 8, 5, 3, 2, 1, 1, whose last square is the gcd, because subtracting the shorter side never changes it. The remainder does many cuts at once (gcd(45, 12) = 3 in three steps), consecutive Fibonacci numbers are the slowest inputs (Lamé's theorem: 85 steps at most up to 10^18, while a million random pairs on this Mac never needed more than 59), and the practical details: lcm as a / gcd * b to avoid overflow, negative remainders in C++, and Stein's binary GCD, which ran about 1.5 times faster than the % loop here.