Math basics: GCD, primes & modular arithmetic

Three small tools carry a lot of number problems: Euclid's algorithm for common divisors, the sieve for primes, and arithmetic mod m to keep huge answers small.

3 examples16 to practiceRead · 5 min
Basic

Euclid's algorithm replaces a pair with a smaller pair

The greatest common divisor of two numbers is the largest number that divides both: gcd(12, 18) = 6. Trying every candidate is slow. Euclid's algorithm uses one fact: any number that divides a and b also divides a mod b, the remainder, and the other way round. So gcd(a, b) = gcd(b, a mod b), and the pair shrinks until the remainder is 0. The other number is then the answer.

Every two rounds the larger number at least halves, so the number of rounds is O(log(min(a, b))): fewer than 90 rounds for numbers up to 10¹⁸.

The least common multiple, the smallest number both divide, follows from it: lcm(a, b) = a ÷ gcd(a, b) × b. Divide before multiplying so the intermediate value stays small.

The rule

gcd(a, b) = gcd(b, a mod b), and gcd(a, 0) = a. Replace, repeat, and you are done in a logarithmic number of steps.

Medium

The sieve crosses out multiples instead of testing each number

A prime number is a whole number above 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11. To test one number n, try dividing by 2, 3, 4, … up to √n: if n = a × b, one of a and b is at most √n. That is O(√n) per number.

To find every prime below n at once, the sieve of Eratosthenes is much faster. Mark every number as a candidate. Take the smallest unmarked-out number, which must be prime, and cross out all its multiples. Repeat. Two details make it quick:

  • start crossing at p × p: every smaller multiple of p has a smaller prime factor and is already crossed out;
  • stop once p × p reaches n: anything still marked is prime.

Each prime p crosses out about n ÷ p numbers, and those sums add up to O(n log log n), close to linear. The cost is O(n) memory for the marks.

Modular arithmetic

Take the remainder after every step, not at the end

Answers that count ways or arrangements grow far past what a 64-bit integer holds, so problems ask for the answer mod some m, often 10⁹ + 7. Modular arithmetic works with remainders only, using rules that let you reduce at every step:

  • (a + b) mod m = ((a mod m) + (b mod m)) mod m
  • (a × b) mod m = ((a mod m) × (b mod m)) mod m
  • (a − b) mod m: add m before taking the remainder, since some languages give negative remainders.

Division has no such rule: dividing mod m needs a modular inverse, a topic of its own.

Reducing after every operation keeps every value below m. With m near 10⁹, the product of two remainders is below about 10¹⁸, which fits in a signed 64-bit integer. JavaScript numbers are exact only up to 2⁵³, about 9 × 10¹⁵, so there such products need BigInt.

Expression

Value

Mod 7

12 + 9

21

0, and (5 + 2) mod 7 = 0

12 × 9

108

3, and (5 × 2) mod 7 = 3

9 − 12

−3

4, after adding 7

Hard

Fast exponentiation squares its way through the bits

Computing x to the power e by multiplying e times is O(e), hopeless for e = 10¹⁸. Fast exponentiation reads e in binary. 217 is 11011001, so 3²¹⁷ = 3¹²⁸ × 3⁶⁴ × 3¹⁶ × 3⁸ × 3¹. Squaring gives 3¹, 3², 3⁴, 3⁸ and so on, one square per bit, and the result multiplies in the squares whose bit is 1. Mod m after every multiplication keeps the numbers small.

One squaring per bit of e and at most one multiplication: O(log e). The same loop computes huge powers mod 10⁹ + 7, matrix powers, and the modular inverse when m is prime.

Check yourself

3 questions

1. What is gcd(84, 36), and how many rounds of Euclid's algorithm does it take?
2. Why does the sieve start crossing out the multiples of p at p × p?
3. A sum is taken mod 10⁹ + 7 only at the very end. What can go wrong?
Takeaways

Remember this

  • Euclid's algorithm: gcd(a, b) = gcd(b, a mod b), O(log min(a, b)) rounds; lcm(a, b) = a ÷ gcd × b.
  • The sieve of Eratosthenes finds every prime below n in O(n log log n), crossing out from p × p.
  • Modular arithmetic reduces after every addition, subtraction and multiplication; division needs an inverse.
  • Fast exponentiation squares once per bit of the exponent: O(log e) multiplications.
Practice

Now solve some yourself

Only this topic 0 of 15 solved

  1. Easy
    Only 2, 3 and 5 as prime factors?
    Math basics: GCD, primes & modular arithmetic
  2. Easy
    Every prime in a range
    Math basics: GCD, primes & modular arithmetic
  3. Easy
    The longest string that divides two strings
    Math basics: GCD, primes & modular arithmetic
  4. Easy
    Split a deck into equal groups of one card value
    Math basics: GCD, primes & modular arithmetic
  5. Medium
    Trailing zeros of n factorial
    Math basics: GCD, primes & modular arithmetic
  6. Medium
    A power tower modulo a prime
    Math basics: GCD, primes & modular arithmetic
  7. Medium
    Numbers with exactly three divisors
    Math basics: GCD, primes & modular arithmetic
  8. Medium
    Measure an exact amount with two jugs
    Math basics: GCD, primes & modular arithmetic
  9. Medium
    Count the divisors of each number
    Math basics: GCD, primes & modular arithmetic
  10. Medium
    The closest pair of primes in a range
    Math basics: GCD, primes & modular arithmetic
  11. Medium
    Raise a number to a power quickly
    Math basics: GCD, primes & modular arithmetic
  12. Medium
    A power with a huge exponent, mod 1337
    Math basics: GCD, primes & modular arithmetic
  13. Medium
    Count digit strings with even and prime places
    Math basics: GCD, primes & modular arithmetic
  14. Medium
    Subarrays whose divisor is exactly k
    Math basics: GCD, primes & modular arithmetic
  15. Hard
    Can some multiples of the list add up to 1?
    Math basics: GCD, primes & modular arithmetic

Mixed with topics you have practiced 0 unlocked

    1 more once you have practiced Binary search on the answer. Play one of its examples to the end and solve one of its problems to unlock them.

    Problems open on their own sites (LeetCode, Codeforces, CodeChef, AtCoder, CSES) in a new tab. Tick Solved when you get it accepted there; your ticks decide which mixed problems open next.

    Next

    Maximum flow

    Continue