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.
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.
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.
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 |
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.
3 questions
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.
Now solve some yourself
Only this topic 0 of 15 solved
- EasyOnly 2, 3 and 5 as prime factors?
- EasyEvery prime in a range
- EasyThe longest string that divides two strings
- EasySplit a deck into equal groups of one card value
- MediumTrailing zeros of n factorial
- MediumA power tower modulo a prime
- MediumNumbers with exactly three divisors
- MediumMeasure an exact amount with two jugs
- MediumCount the divisors of each number
- MediumThe closest pair of primes in a range
- MediumRaise a number to a power quickly
- MediumA power with a huge exponent, mod 1337
- MediumCount digit strings with even and prime places
- MediumSubarrays whose divisor is exactly k
- HardCan some multiples of the list add up to 1?
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