Short answer: Tonelli-Shanks computes a square root of a modulo an odd prime p, given that a is a quadratic residue. Write p−1 = q·2^s with q odd. Find a quadratic nonresidue z, set an initial guess from a^{(q+1)/2}, and then walk a 2-power tower that corrects the guess…
Category: Number Theory
How the sieve of Eratosthenes strikes composites
Short answer: The sieve of Eratosthenes lists every prime up to n by striking multiples. For each prime p whose square is still at most n, it marks p·p, p·p+p, p·p+2p, and so on. Multiples below p² were already marked by smaller primes. The algorithm runs in O(n log…
How the Lucas-Lehmer test checks a Mersenne number
Short answer: The Lucas-Lehmer test decides whether a Mersenne number M = 2^p − 1 is prime, for an odd prime p. It sets s = 4 and repeats s = s² − 2, modulo M, p−2 times. M is prime exactly when the final s is 0 modulo…
How the linear sieve marks each composite once
Short answer: The linear sieve computes the smallest prime factor of every integer up to n in O(n) arithmetic steps. It walks i from 2 to n, records i as prime when it has no factor yet, and for each known prime p marks p·i, stopping when p divides…
How the extended Euclidean algorithm writes Bezout
Short answer: The extended Euclidean algorithm returns integers x and y such that a·x + b·y = gcd(a, b). It runs the same divisions as the Euclidean algorithm and also updates two coefficient rows, one for each remainder. The modular inverse of a modulo m exists exactly when the…
How the Euclidean algorithm divides out a gcd
Short answer: The Euclidean algorithm computes gcd(a, b) by replacing the pair with (b, a mod b) until the remainder is 0. The other number is the gcd. It runs in O(log min(a, b)) division steps, and the worst case is a pair of consecutive Fibonacci numbers, which makes…
How the Chinese remainder theorem builds a residue
Short answer: The Chinese remainder theorem says that if the moduli m1, …, mk are pairwise coprime, then for any residues a1, …, ak there is one solution modulo M = m1·…·mk of the system x ≡ ai (mod mi). The constructive form computes, for each i, the product…
How the AKS test proves primality in polynomial time
Short answer: The AKS primality test decides whether an integer n is prime in time polynomial in the number of bits of n. It generalizes the observation that n is prime exactly when (x−1)^n ≡ x^n − 1 (mod n) as a polynomial, and it checks a related congruence…
How Pollard’s rho hunts a factor on a cycle
Short answer: Pollard’s rho looks for a nontrivial factor of a composite n by iterating a polynomial, usually x² + c, modulo n, and taking gcd of n with the difference of two values on the sequence. By the birthday paradox the sequence cycles quickly modulo a secret prime…
How Miller-Rabin tests a strong pseudoprime
Short answer: The Miller-Rabin test writes an odd number n as d·2^s + 1 and looks at a^d mod n. The test passes for a base a when that power is 1, or when some a^{d·2^r} mod n equals n−1 for an r before s. A prime passes for…
How binary exponentiation squares a power
Short answer: Binary exponentiation computes a^e with O(log e) multiplications by scanning the bits of the exponent. The square-and-multiply loop squares a running power for every bit and multiplies by the base when the bit is 1. The same loop with every multiplication reduced modulo m computes a^e mod…
How baby-step giant-step solves a discrete log
Short answer: Baby-step giant-step solves g^x ≡ h (mod p) for a small x by writing x = i·m − j with m = ceil(sqrt(N)) and x in a range of length N. It stores the baby steps g^j for j = 0..m−1 in a hash table, then walks…
