⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 28s17 days 22 hours 20 minutes 28 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 28s17 days 22 hours 20 minutes 28 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.