Prime numbers — integers greater than 1 divisible only by 1 and themselves — are the atoms of number theory. Every integer greater than 1 is either prime or can be expressed uniquely as a product of prime numbers (the Fundamental Theorem of Arithmetic). This primality property makes primes foundational in cryptography (RSA encryption's security depends on the computational difficulty of factoring large numbers into primes), computer science (hash functions, random number generation), and mathematics across dozens of specialized areas. Knowing how to determine whether a number is prime — efficiently, for both small and large numbers — is a practical skill with real computational applications.
Fermat's Little Theorem and Probabilistic Tests
For large numbers (hundreds of digits), trial division is computationally infeasible, and even the sieve fails on memory requirements. Probabilistic primality tests provide fast answers with very high confidence. Fermat's Little Theorem: if p is prime and a is not divisible by p, then a^(p-1) ≡ 1 (mod p). The Fermat primality test checks this for several random values of a — if any fail, p is definitely composite; if all pass, p is probably prime.
The Miller-Rabin primality test is the industry-standard probabilistic test. For each randomly chosen base a: write p-1 = 2ˢ × d (odd d). Compute a^d mod p. If the result is 1 or p-1, this base passes. Otherwise, square it repeatedly up to s times — if it ever reaches p-1, it passes. If none of the squarings reach p-1, p is composite. Each base that passes reduces the probability of false positives by factor 4. With 20 random bases, the probability that a composite number passes all 20 tests is less than 4^(-20) ≈ 10^(-12). Used in SSL/TLS certificate generation, the Miller-Rabin test determines primality of 2048-bit and 4096-bit candidate primes in milliseconds.
Related Calculators
- Factorial Calculator
- Fibonacci Number Calculator