Advertisement

Previous Prime Calculator

Find the largest prime number smaller than any whole number up to 24 digits, with the gap to your number and the ten primes just below it.

A whole number greater than 2, up to 24 digits. Commas and spaces are ignored.

This tool finds the previous prime: the largest prime number that is strictly smaller than the number you enter. It is handy for choosing hash-table sizes, checking homework on prime gaps, or picking a prime modulus just under a power of two or ten. It handles whole numbers up to 24 digits exactly and also lists the ten primes just below your number.

How to use this tool

  1. Type a whole number greater than 2. Commas are fine, so 1,000,000 works.
  2. Read the previous prime and how far below your number it is.
  3. See whether your number is itself prime.
  4. Scan the table for the next primes further down.

Formula

prevprime(n) = max { p prime : p < n }

The calculator counts down from n − 1, skipping even numbers, and tests each candidate. A candidate is prime if it passes a Miller–Rabin test for the bases 2, 3, 5, …, 41; with these thirteen bases the test has no false positives for any number below about 3.3 × 10²⁴, so the answer is certain, not probabilistic.

Example

For n = 1,000,000 the calculator checks 999,999 (divisible by 3), 999,997 (= 757 × 1,321), and so on down to 999,983, the first prime it meets. The gap is 1,000,000 − 999,983 = 17. The next primes further down are 999,979 and 999,961.

Frequently asked questions

If my number is prime, is it its own previous prime?

No. The previous prime is always strictly smaller. For 97 the answer is 89, and the tool also tells you that 97 itself is prime.

Why is there no previous prime for 2?

2 is the smallest prime, so no prime is smaller than 2. For n = 3 the answer is 2.

How large can the gap between primes be?

On average the gap near n is about ln(n), so roughly 14 near one million and 28 near a trillion, but individual gaps vary a lot and can be much larger.

How big a number can I enter?

Up to 24 digits. The result is exact in that whole range because the primality test is deterministic there.

Last updated: 2026-10-30

Advertisement