← Laboratorium Math · experiment

Math · numbers and secrets

Prime numbers and ciphers the atoms of arithmetic

A prime number is divisible only by 1 and by itself. Multiply such “atoms” together and you can build every whole number — in exactly one way. Run numbers through the sieve of Eratosthenes, spot the diagonal streaks in the Ulam spiral, do sums on a clock face, crack a Caesar cipher like a medieval codebreaker, and then build your own RSA key — the same idea that powers the padlock in your browser’s address bar today.

n = p1a1 · p2a2 ⋯ ap−1 ≡ 1 (mod p) c = me mod n π(x) ≈ xln x
Q–Y modes Challenges 0/6

Laboratorium · sieve of Eratosthenes

Grid range

Click the smallest number not yet crossed out that is greater than 1 — the sieve will cross out all of its multiples. Or let the sieve run by itself.

Primes—
Composites crossed out—
Now crossing out multiples of—
Limit √N—
Selected numberhover over a cell

Factor tree

—

A molecule of prime atoms

Trial division for small divisors, the Miller–Rabin test and Pollard’s rho method for large ones (BigInt).

Number of divisors—
Sum of divisors—

All divisors

Why isn’t 1 a prime? If it were, factorization wouldn’t be unique: 6 = 2 · 3 = 1 · 2 · 3 = 1 · 1 · 2 · 3… That’s why 1 is neither prime nor composite.

MathThe atoms of arithmetic

Prime and composite numbers

A prime number is a natural number greater than 1 that has exactly two divisors: 1 and itself (2, 3, 5, 7, 11, 13…). The other numbers greater than 1 are composite numbers. The fundamental theorem of arithmetic says that every number greater than 1 can be written as a product of primes in exactly one way (up to the order of the factors) — like a molecule built from atoms.

360 = 2³ · 3² · 5, 1001 = 7 · 11 · 13, the number of divisors of n = paqb⋯ is (a+1)(b+1)⋯

The sieve of Eratosthenes and the square root

Write out the numbers 2…N, take the smallest one not yet crossed out, p (it is prime), and cross out its multiples, starting from p² — smaller multiples have a smaller divisor and are already gone. Once p² > N, we can stop: a composite number n ≤ N always has a prime divisor ≤ √n.

How many primes are there?

π(x) ≈ xln x, Li(x) = ∫2x dtln t

Infinitely many — as Euclid proved. The prime number theorem (Hadamard and de la Vallée Poussin, 1896) says that among numbers around x, roughly one in every ln x is prime. The logarithmic integral Li(x) approximates π(x) even better.

Clock arithmetic, GCD and LCM

a ≡ b (mod m) ⇔ m | (a − b), GCD(a, b) = GCD(b, a mod b), LCM(a, b) = abGCD(a, b)

Numbers that leave the same remainder when divided by m are congruent (that’s a congruence). On a dial with m positions, every nonzero number has an inverse if and only if m is prime. Fermat’s little theorem: if p is prime and p does not divide a, then ap−1 ≡ 1 (mod p). Euclid’s algorithm replaces the larger number with the remainder of the division until the remainder is 0 — the last nonzero remainder is the GCD.

RSA in five lines

n = pq, φ(n) = (p − 1)(q − 1), e · d ≡ 1 (mod φ(n)), c = me mod n, m = cd mod n

The public key (n, e) is an open padlock that anyone can snap shut. The private key d opens it. Computing d requires φ(n), and that requires knowing p and q. It works thanks to Euler’s theorem: med ≡ m (mod n). A digital signature swaps the roles of the keys, and a hash function produces a short fingerprint, which is what gets signed.

Simplifications on this page

We do RSA with small numbers and letter by letter, without random padding — it’s a model for learning, not a secure cipher. Mixing paints in Diffie–Hellman is only a metaphor. The HTTPS diagram skips many steps of the TLS protocol. The password rating is an upper estimate of entropy: passwords people make up are usually much weaker. English letter frequencies are approximate (they depend on the kind of text). The RSA cracking-time chart is an order-of-magnitude extrapolation.

Did you know…?

  • There are infinitely many primes. Euclid’s proof fits in a single sentence: multiply all the known primes together and add 1 — the result isn’t divisible by any of them, so it has a prime divisor that isn’t on the list.
  • The Ulam spiral was supposedly invented out of boredom: in 1963 Stanisław Ulam was doodling numbers on grid paper during a long lecture and circled the primes. The diagonal streaks surprised even him.
  • Polish mathematicians broke Enigma as early as 1932 — Marian Rejewski reconstructed its rotors by solving permutation equations, long before Enigma became famous during the war.

Challenges

  • Sieve by handCross out every composite number up to 100 with the sieve, clicking the successive primes yourself.not completed
  • Split into atomsFactor 1001 or any composite number with at least 6 digits.not completed
  • Goldbach beyond a thousandFind, on your own, a prime p for which an even number greater than 1000 equals p + (another prime).not completed
  • Crack CaesarIn the frequency analysis attack, line up the bars and read the puzzle.not completed
  • A letter via RSAEncrypt a message with the public key and decrypt it with the private key.not completed
  • A shared secretAgree on a shared secret with Bob using Diffie–Hellman, choosing your own secret numbers.not completed

Challenges check themselves. Progress is saved in this browser.

See also

The logarithm from “Functions” describes the density of primes (n / ln n), and the gears of a watch are LCM in practice.