Prime Number Checker
Check if any number is prime using an optimized trial division algorithm. Also lists factors.
What Is the Prime Number Checker?
A prime number is an integer greater than 1 that has no positive divisors other than 1 and itself. Prime numbers are the multiplicative building blocks of all integers, every integer greater than 1 is either prime or can be uniquely expressed as a product of primes (Fundamental Theorem of Arithmetic).
- ›Trial division, the algorithm tests divisibility only up to √n. If no factor is found below √n, the number is prime. Testing 6k±1 patterns skips all multiples of 2 and 3, reducing work by ~67%.
- ›Prime factorization, if composite, the result includes the full factorization in exponent notation (e.g., 360 = 2³ × 3² × 5).
- ›Neighbor primes, the calculator finds the nearest prime below and above the input, plus the prime gap (next − prev).
- ›Special categories, twin, safe, Sophie Germain, and Mersenne primes are identified with color-coded badges, making this a useful reference for number theory students.
Formula
Definition
n is prime if n ≥ 2 and has no divisors other than 1 and n.
Trial Division Algorithm
Test divisibility by 2 and 3, then all numbers of form 6k±1 up to √n.
Complexity: O(√n)
Special Prime Types
Twin prime: both n and n±2 are prime
Safe prime: n and (n−1)/2 are both prime
Sophie Germain: n and 2n+1 are both prime
Mersenne prime: n = 2ᵏ − 1 for some integer k
| Type | Condition | Example |
|---|---|---|
| Twin prime | n and n+2 (or n−2) both prime | (17, 19), (41, 43) |
| Safe prime | n prime and (n−1)/2 prime | 11, 23, 47, 59 |
| Sophie Germain | n prime and 2n+1 prime | 2, 3, 5, 11, 23 |
| Mersenne prime | n = 2^k − 1 for integer k | 7 (2³−1), 31 (2⁵−1) |
| Composite | Has a factor between 2 and n−1 | 91 = 7 × 13 |
How to Use
- 1Select "Check a Number" tab to test a single integer, or "List Primes in Range" to find all primes in an interval.
- 2In single mode: enter any positive integer up to 1,000,000,000 and press Check (or Enter).
- 3The result shows PRIME or COMPOSITE, the prime factorization, adjacent primes, prime gap, and any special prime badges.
- 4In range mode: enter a start (A ≥ 2) and end (B, max A+10,000) value, then press List Primes.
- 5The range result lists all primes in [A, B] with the count, smallest, and largest prime shown.
- 6Press Clear to reset all fields and remove saved values.
Example Calculation
Checking 17:
Checking 91:
Famous Primes
- ›2, the only even prime
- ›7, Mersenne prime (2³−1)
- ›31, Mersenne prime (2⁵−1)
- ›127, Mersenne prime (2⁷−1)
- ›8,191, Mersenne prime (2¹³−1)
- ›999,983, largest prime below 1,000,000
Understanding Prime Number Checker
The Distribution of Prime Numbers
Primes appear irregularly among integers, yet their distribution follows deep statistical laws. The Prime Number Theorem (proved independently by Hadamard and de la Vallée Poussin in 1896) shows that the number of primes up to n is approximately n / ln(n). There are 25 primes below 100, 168 below 1,000, 78,498 below 1,000,000, and about 50.8 million below 1 billion.
Prime Numbers Below 100
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97
25 primes below 100
Frequently Asked Questions
Why only test divisors up to √n?
The √n bound makes primality testing efficient:
- ›Mathematical proof, suppose n = a × b where a > √n. Then b = n/a < n/√n = √n. So b < √n, meaning b would have been found before a in our test.
- ›Practical impact, for n = 1,000,000,000 (1 billion), we only need to test up to √(10⁹) ≈ 31,623. That is only 31,623 divisions instead of nearly a billion.
- ›Further optimization, testing 6k±1 (i.e., 5, 7, 11, 13, 17, 19, …) skips all multiples of 2 and 3 first, cutting the work by ⅔ before we even start the loop.
- ›Performance, this calculator handles numbers up to 1 billion near-instantly because trial division up to ~31,623 is trivial for modern hardware.
What is a twin prime?
Twin primes are among the most studied structures in number theory:
- ›Definition, a pair (p, p+2) where both p and p+2 are prime. The smallest twin prime pair is (3, 5).
- ›Examples, (3,5), (5,7), (11,13), (17,19), (29,31), (41,43), (59,61), (71,73).
- ›Twin Prime Conjecture, believed to be true but unproven. Yitang Zhang's landmark 2013 paper proved infinitely many prime pairs with gap ≤ 70,000,000, since narrowed to 246 by the Polymath project.
- ›Density, twin primes become rarer as numbers grow, but appear to continue infinitely. The density near n is approximately 2C₂ / (ln n)² where C₂ ≈ 0.6601618 is the twin prime constant.
What is a Mersenne prime?
Mersenne primes are rare and computationally significant:
- ›Form, Mₖ = 2ᵏ − 1. For Mₖ to be prime, k itself must be prime (though prime k does not guarantee Mₖ is prime, e.g., 2¹¹−1 = 2047 = 23 × 89).
- ›Known examples, 3, 7, 31, 127, 8191, 131071, 524287, … The 51st known Mersenne prime (found 2024) has over 41 million digits.
- ›GIMPS, the Great Internet Mersenne Prime Search is a distributed computing project that has found all recent record primes since 1996.
- ›Connection to perfect numbers, every Mersenne prime Mₖ generates an even perfect number: 2^(k-1) × Mₖ. For example, M₂ = 3 gives the perfect number 2¹ × 3 = 6.
Are there infinitely many prime numbers?
Euclid's proof of the infinitude of primes is one of the oldest and most elegant in mathematics:
- ›The proof, assume primes are finite: p₁, p₂, …, pₙ. Let N = p₁×p₂×⋯×pₙ + 1. Then N mod pᵢ = 1 for every prime in our list, so no listed prime divides N.
- ›Conclusion, N is either prime itself or has a prime factor not in our list. Either way, the list was not complete. Contradiction, so there cannot be a finite list of all primes.
- ›Prime counting function, π(n) counts primes up to n. The Prime Number Theorem says π(n) ≈ n/ln(n). There are 78,498 primes below 1,000,000 and about 50,847,534 below 10⁹.
- ›Unsolved questions, whether infinitely many twin primes, Sophie Germain primes, and Mersenne primes exist all remain open conjectures despite the infinity of ordinary primes.
What is a safe prime?
Safe primes have both mathematical and cryptographic significance:
- ›Definition, a prime p where (p−1)/2 is also prime. The linked pair: (p−1)/2 is the Sophie Germain prime; p is the safe prime.
- ›Examples, 11 (Sophie: 5), 23 (11), 47 (23), 59 (29), 83 (41), 107 (53), 167 (83), 179 (89).
- ›Cryptography, safe primes are ideal for Diffie–Hellman key exchange because the multiplicative group mod p has only trivial small subgroups (order 1, 2, and (p−1)/2). This prevents small subgroup attacks.
- ›Rarity, safe primes become increasingly sparse as numbers grow, though infinitely many are conjectured to exist (unproven).
What is the prime gap?
Prime gaps measure the spacing between consecutive primes:
- ›Definition, if p and q are consecutive primes (q is the next prime after p), the prime gap is q − p.
- ›Small gaps, gap of 1 only for (2,3). Gap of 2 for twin primes like (5,7), (11,13), (17,19). The smallest gap for primes > 2 is 2.
- ›Large gaps, for any k, we can find k consecutive composites: (k+1)! + 2, (k+1)! + 3, …, (k+1)! + (k+1) are all composite. So gaps can be as large as desired.
- ›Average gap, the Prime Number Theorem tells us primes near n have average spacing ≈ ln(n). Near 1,000,000 the average gap is ≈ ln(10⁶) ≈ 13.8.
Why are prime numbers important in cryptography?
Primes are the mathematical foundation of internet security:
- ›RSA encryption, the public key is n = p × q for two large secret primes. Encryption and decryption use modular arithmetic with n. Security depends on factoring n being computationally intractable.
- ›Factoring hardness, the best classical algorithms (GNFS) can factor a 768-bit number in about a year of supercomputer time. RSA-2048 is considered secure beyond 2040.
- ›Diffie-Hellman, uses a large prime p and a generator g to enable two parties to establish a shared secret over a public channel without exchanging the secret directly.
- ›Quantum threat, Shor's algorithm runs on a quantum computer and factors n in polynomial time. Post-quantum cryptography standards (NIST PQC, 2024) are being deployed to replace RSA before large quantum computers exist.