AlgoViz

GCD & LCM

easy 14 min

The GCD is the largest number that divides two numbers; the LCM is the smallest number both divide.

Where it shows up

The same pattern appears across everyday software.

  • RSA encryption

    RSA chooses a public exponent e with gcd(e, phi(n)) = 1 and computes the private key as e's inverse modulo phi(n) using the extended Euclidean algorithm. Euclid's speed on numbers hundreds of digits long is what makes key generation practical.

  • Fractions and exact arithmetic

    Python's fractions.Fraction and every computer algebra system divide numerator and denominator by their GCD after each operation, so 12/18 is stored as 2/3 and numbers stay small.

  • Scheduling and periodic jobs

    Traffic lights, cron jobs and real-time task schedulers with different periods repeat their combined pattern every LCM of the periods. Real-time systems call that the hyperperiod and check deadlines over one of them.

  • Hash tables and random number generators

    A probe step that is coprime with the table size visits every slot before repeating, which is why open-addressing tables often use prime sizes. Linear congruential generators get their full period under similar coprimality rules.

  • Screens, images and gears

    A 1920 by 1080 screen has aspect ratio 16:9 because gcd(1920, 1080) = 120. Gear designers pick tooth counts with a small GCD so the same teeth meet rarely and wear evenly; the pair realigns every LCM rotations.