Prime Number Calculator

Check primality, compute prime factorization, and find all primes within a number range.

Reviewed by CalculatorApp.me Math Team

Prime Number Calculator — Complete Guide

Primality testing, factorization, sieves, and the role of primes in cryptography and mathematics.

168

Primes below 1000

2

Only even prime

∞

Infinitely many primes

RSA

Crypto uses primes

What Are Prime Numbers?

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. Prime numbers are the "atoms" of arithmetic — every integer greater than 1 can be uniquely expressed as a product of primes (the Fundamental Theorem of Arithmetic).

Composite numbers have additional factors: 12 = 2² × 3, 100 = 2² × 5². The number 1 is neither prime nor composite by modern convention. The number 2 is the only even prime — all other even numbers are divisible by 2.

Primes are central to modern cryptography: RSA encryption relies on the fact that multiplying two large primes is easy, but factoring their product is computationally infeasible. A 2048-bit RSA key uses primes with ~300 digits each. This asymmetry secures online banking, HTTPS, and digital signatures worldwide.

Primality Testing Methods

Trial Division
Algorithm: Test divisibility by all
integers from 2 to √n

def is_prime(n):
  if n < 2: return False
  if n < 4: return True
  if n % 2 == 0 or n % 3 == 0:
    return False
  i = 5
  while i * i <= n:
    if n % i == 0 or n % (i+2) == 0:
      return False
    i += 6
  return True

Why only test up to √n?
  If n = a × b, then min(a,b) ≤ √n
  So if no factor ≤ √n, n is prime

Complexity: O(√n)
  For n = 1,000,000 → test ~1000 values
  Practical for n up to ~10¹²

Trial division is the simplest primality test. The optimization of testing only 6k±1 (skipping multiples of 2 and 3) reduces checks by 67%. Sufficient for everyday use.

Sieve of Eratosthenes
Find ALL primes up to N:

1. List numbers 2, 3, 4, ..., N
2. Start with smallest (2)
3. Mark all multiples of 2 as composite
4. Move to next unmarked number (3)
5. Mark all multiples of 3
6. Repeat until √N reached
7. Remaining unmarked = PRIMES

Sieve of Eratosthenes (up to 30):
  2  3  4̶  5  6̶  7  8̶  9̶  10̶
  11 12̶ 13 14̶ 15̶ 16̶ 17 18̶ 19
  20̶ 21̶ 22̶ 23 24̶ 25̶ 26̶ 27̶ 28̶
  29 30̶

Primes: 2,3,5,7,11,13,17,19,23,29

Complexity: O(n log log n)
Memory: O(n) → segmented for large n

The Sieve of Eratosthenes (240 BC) is still one of the fastest algorithms for generating all primes up to N. For primes below 10⁹, segmented sieves run in seconds on modern hardware.

Miller-Rabin Probabilistic Test
Probabilistic primality test:
  'Probably prime' or 'definitely composite'

Basis: Fermat's Little Theorem
  If p is prime: a^(p-1) ≡ 1 (mod p)

Miller-Rabin extends this:
  Write n−1 = 2ˢ × d (d odd)
  Choose random witness a
  Compute a^d mod n
  If = 1 or n−1: probably prime
  Square up to s−1 times:
    If any = n−1: probably prime
  Otherwise: COMPOSITE (certain)

Error probability: ≤ (1/4)^k
  k witnesses → P(error) ≤ 4⁻ᵏ
  20 witnesses → P(error) < 10⁻¹²

Deterministic variant:
  Test a = 2,3,5,7,11,13,17,19,23,29,31,37
  Correct for all n < 3.317×10²⁴

Miller-Rabin is the standard primality test in practice. With enough witnesses, false positives are astronomically unlikely. Most crypto libraries use Miller-Rabin with 40+ rounds for key generation.

Prime Factorization
Express n as product of primes:

Trial Division Factorization:
  360 ÷ 2 = 180
  180 ÷ 2 = 90
  90  ÷ 2 = 45
  45  ÷ 3 = 15
  15  ÷ 3 = 5
  5   ÷ 5 = 1
  360 = 2³ × 3² × 5

Applications:
  GCD(a,b): min of each prime power
  LCM(a,b): max of each prime power
  GCD(12,18) = GCD(2²×3, 2×3²)
             = 2¹×3¹ = 6
  LCM(12,18) = 2²×3² = 36

Number of divisors:
  n = p₁^a₁ × p₂^a₂ × ...
  d(n) = (a₁+1)(a₂+1)...
  360 = 2³×3²×5¹
  d(360) = 4×3×2 = 24 divisors

Cryptographic factoring:
  RSA-2048: ~617 digit number
  No known efficient algorithm!

Factoring large numbers is computationally hard — this is the foundation of RSA encryption. If quantum computers achieve sufficient scale, Shor's algorithm could factor in polynomial time, breaking RSA.

Special Types of Primes

TypeFormExamplesKnown CountSignificance
Mersenne2ᵖ − 13, 7, 31, 12751 knownLargest known primes
Twinp, p+2(3,5), (11,13), (41,43)∞ conjecturedTwin prime conjecture
Sophie Germainp & 2p+1 prime2, 3, 5, 11, 23∞ conjecturedCryptographic use
Fermat2^(2ⁿ)+13, 5, 17, 257, 655375 confirmedConstructible polygons
PalindromicReads same both ways11, 101, 131, 151∞ conjecturedRecreational math
Safe(p−1)/2 is prime5, 7, 11, 23, 47∞ conjecturedStrong crypto keys
EmirpPrime reversed too13↔31, 17↔71∞ conjecturedNumber theory
Gaussiana+bi, irreducible3, 7, 11, 2+i∞Complex primes

Prime Distribution

RangePrime CountDensityLargest Prime in Range
1 – 1002525%97
1 – 1,00016816.8%997
1 – 10,0001,22912.3%9,973
1 – 100,0009,5929.6%99,991
1 – 1,000,00078,4987.8%999,983
1 – 10⁹50,847,5345.1%999,999,937
1 – 10¹²37,607,912,0183.8%999,999,999,989

Density decreases logarithmically: π(x) ≈ x/ln(x) (Prime Number Theorem). Primes never run out, but they thin out at a predictable rate.

History of Prime Numbers

~300 BC

Euclid — Infinitely Many Primes

In Elements (Book IX, Prop. 20), Euclid proved there are infinitely many primes with an elegant contradiction argument still taught today: assume finitely many, multiply them all and add 1 — the result is not divisible by any known prime. Therefore another prime must exist.

~240 BC

Eratosthenes — The First Prime Sieve

Eratosthenes of Cyrene created the first systematic algorithm for finding primes. His sieve method — crossing out multiples of each prime starting from 2 — remains one of the most efficient algorithms for generating all primes up to N. Still used in competitive programming 2,300 years later.

1640

Fermat — Fermat's Little Theorem

Pierre de Fermat stated (without proof) that if p is prime, then aᵖ⁻¹ ≡ 1 (mod p). Euler later proved it. This theorem became the basis for probabilistic primality tests (Fermat test, Miller-Rabin) still used in modern cryptography.

1796

Gauss & Legendre — Prime Number Theorem Conjecture

Both Gauss and Legendre independently conjectured that π(x) ≈ x/ln(x) — the number of primes below x grows inversely with the natural logarithm. This was proved rigorously in 1896 by Hadamard and de la Vallée-Poussin using complex analysis.

1977

RSA — Primes Secure the Internet

Rivest, Shamir, and Adleman published the RSA cryptosystem, which relies on the difficulty of factoring products of large primes. If n = p × q with ~300-digit primes, finding p and q from n is computationally infeasible. RSA secures nearly all internet communication.

2024

Largest Known Prime: 2⁸²,⁵⁸⁹,⁹³³ − 1

The largest known prime as of 2024 has 24,862,048 digits — discovered by the Great Internet Mersenne Prime Search (GIMPS). It would take ~8,800 pages to print. The search for large primes continues as both mathematical pursuit and distributed computing project.

Key Research & Data

Myths vs. Facts

✕

There is a formula that generates all prime numbers.

✓

No known formula efficiently generates all primes. While formulas exist (like Wilson's theorem: p is prime iff (p−1)!+1 divisible by p), they're computationally useless. The distribution of primes follows patterns (PNT) but individual primes appear unpredictably.

✕

1 is a prime number.

✓

By modern convention (established ~1900s), 1 is NOT prime. If 1 were prime, the Fundamental Theorem of Arithmetic would fail — factorizations wouldn't be unique (12 = 2²×3 = 1×2²×3 = 1²×2²×3...). Excluding 1 preserves unique factorization.

✕

Larger numbers are less likely to be prime, so primes eventually run out.

✓

Primes thin out (density ≈ 1/ln(n)) but NEVER stop. Euclid proved infinitely many primes exist ~300 BC. No matter how large a number you pick, there's always another prime beyond it. The thinning is logarithmic, not finite.

✕

Quantum computers will immediately break all encryption.

✓

Shor's algorithm can theoretically factor large numbers in polynomial time, threatening RSA. But current quantum computers have ~1,000 qubits; breaking RSA-2048 requires millions of error-corrected qubits. Post-quantum cryptography (lattice-based) is already being deployed as a precaution.

Frequently Asked Questions

How do I check if a number is prime?▼
For small numbers: divide by all primes up to √n. If none divide evenly, it's prime. For large numbers: use Miller-Rabin probabilistic test. For guaranteed answers on very large numbers: AKS (polynomial time, but slow in practice).
What are the factors of a number, like 40, 27, or 50?▼
A number's factors are every whole number that divides it with no remainder — not just its prime factors. This calculator's Prime Factorization mode gives you the prime building blocks; the complete factor list is built by combining those prime factors in every combination. For example: 40 = 2³ × 5, so its factors are 1, 2, 4, 5, 8, 10, 20, 40 (8 factors). 27 = 3³, so its factors are 1, 3, 9, 27. 21 = 3 × 7, so its factors are 1, 3, 7, 21. 49 = 7², so its factors are 1, 7, 49. 50 = 2 × 5², so its factors are 1, 2, 5, 10, 25, 50. 80 = 2⁴ × 5, so its factors are 1, 2, 4, 5, 8, 10, 16, 20, 40, 80.
Why is 2 the only even prime?▼
Every even number > 2 is divisible by 2, making it composite. 2 itself has no divisors other than 1 and 2, so it's prime. This makes 2 unique: the smallest prime, the only even prime, and the only prime that's not odd.
What is the Fundamental Theorem of Arithmetic?▼
Every integer > 1 can be expressed as a product of prime numbers in exactly one way (up to ordering). Example: 360 = 2³ × 3² × 5 — no other prime factorization exists. This uniqueness is why primes are called the 'building blocks' of numbers.
What are twin primes?▼
Pairs of primes differing by 2: (3,5), (5,7), (11,13), (17,19), (29,31), (41,43)... The Twin Prime Conjecture states there are infinitely many such pairs. Unproven, but Zhang (2013) proved infinitely many pairs with gap ≤ 70 million (now ≤ 246).
How does RSA encryption use primes?▼
RSA: pick two large primes p,q; compute n=p×q (public); use Euler's totient φ(n)=(p−1)(q−1) for key generation. Encryption/decryption uses modular exponentiation. Security relies on the fact that factoring n back into p×q is computationally infeasible.
What is the largest known prime number?▼
As of 2024: 2^82,589,933 − 1, a Mersenne prime with 24,862,048 digits, found by GIMPS. It would fill ~8,800 pages if printed. The search continues — anyone can help via the GIMPS distributed computing project at mersenne.org.
What is the Sieve of Eratosthenes?▼
An algorithm from 240 BC that finds all primes up to N: list all numbers, then systematically cross out multiples of 2, 3, 5, 7... up to √N. Remaining uncrossed numbers are prime. Time: O(n log log n). Still one of the fastest methods.
Do primes follow a pattern?▼
No simple pattern generates all primes, but their statistical distribution is well understood: π(n) ≈ n/ln(n) (Prime Number Theorem). Primes appear random locally but follow logarithmic thinning globally. The Riemann Hypothesis, if true, would give even tighter bounds.
What is the Goldbach Conjecture?▼
Every even number > 2 can be expressed as the sum of two primes. Examples: 4=2+2, 6=3+3, 8=3+5, 10=5+5. Verified computationally up to 4×10¹⁸ but never proved. One of the oldest unsolved problems in mathematics (1742).
What are Mersenne primes?▼
Primes of the form 2ᵖ−1, where p itself must be prime. Examples: 2²−1=3, 2³−1=7, 2⁵−1=31. Not all 2ᵖ−1 are prime (2¹¹−1=2047=23×89). Only 51 Mersenne primes are known. They're computationally efficient to test via the Lucas-Lehmer test.
What is a prime factorization tree?▼
A visual method: divide a number by its smallest prime factor, then repeat for the quotient. Example: 60 → 2×30 → 2×2×15 → 2×2×3×5. The leaves are the prime factors: 60 = 2²×3×5. Useful for teaching and finding GCD/LCM.

References

Related Calculators

Explore All Math Calculators

Precision math tools for students, cryptographers, and number theorists — CalculatorApp.me.

Browse Math Calculators →

Prime Number Calculator — Answer & Method

Check if a number is prime, find prime factors, and generate lists of prime numbers.

Formula: Prime Test

Check divisibility for all primes ≤ √n

n = Number

Example Calculation

97 is prime — it has no divisors other than 1 and itself.

Important limitation

Check the result against your specific circumstances before making a decision.

Key Facts

  • A prime number has exactly two distinct positive divisors: 1 and itself.
  • 2 is the only even prime number.

Sources & Validation

Number Theory (Hardy & Wright)OEIS

Related Calculators

Deterministic: YesAI-Generated Numbers: NoVerified: 2026-02-12

Related Articles

See Also