AlgoViz

Prime numbers

easy 15 min

A prime is a whole number above 1 whose only divisors are 1 and itself.

Where it shows up

The same pattern appears across everyday software.

  • Public-key encryption

    RSA keys are built from two random primes of about 1,024 bits each. Their product is published; the security rests on how hard it is to factor that product back into the two primes. Generating the primes uses fast probabilistic tests, since roughly one in 355 random odd 1,024-bit numbers is prime.

  • Hash tables

    Many hash table implementations that reduce with key % size pick a prime size, so keys that share a common step, such as multiples of 10, spread across buckets instead of piling into a few.

  • Random number generators

    Linear congruential generators compute x(n+1) = (a * x(n) + c) mod m. The classic MINSTD generator uses the prime modulus 2^31 - 1, which with a good multiplier gives a period of over two billion values.

  • String hashing and Rabin-Karp

    Rolling hashes for substring search and duplicate detection evaluate a polynomial modulo a large prime such as 10^9 + 7. A prime modulus makes every non-zero value invertible and keeps collisions rare.

  • Competitive programming answers

    Problems with huge counts ask for the answer modulo 10^9 + 7, a prime, so division can be done with modular inverses by Fermat's little theorem.