Next Prime Number Calculator
Find the next prime after any whole number up to 10^24, with the previous prime, the gap and the next ten primes, using a proven primality test.
Type any whole number and this calculator returns the smallest prime greater than it, the largest prime below it, the size of the gap and the ten primes that follow. It accepts numbers up to 10^24, so finding the first prime after a trillion or after 10^18 is supported with exact integer arithmetic.
How to use this tool
- Enter your number; commas, 1e12 and 10^12 are all fine.
- Tick the box if a prime input should count as its own answer.
- Read the next prime, the previous prime and the gap.
- Use the table for the ten primes that come next and the gaps between them.
Formula
next prime(n) = the smallest p > n such that p is prime
The calculator tests n + 1, n + 2, … in turn. Each candidate is first divided by the primes below 1,000; survivors go through the Miller–Rabin test with the 13 prime bases 2, 3, 5, …, 41. That set of bases is proven to give no false positives for any number below 3.3 × 10^24, so every answer is a certain prime, not just a probable one. Bertrand's postulate guarantees a prime between n and 2n for every n > 1.
Example
After 1,000,000 the next prime is 1,000,003: 1,000,001 = 101 × 9,901 and 1,000,002 is even. The previous prime is 999,983, and the gap from your number to the next prime is 3. After 1,000,000,000 the next prime is 1,000,000,007, a modulus programmers use often.
Frequently asked questions
What is the next prime after 0, 1 or a negative number?
Neither 0 nor 1 is prime, and primes are positive, so the answer for all of them is 2 — the smallest and only even prime.
How far apart are consecutive primes?
Near n the average gap is about ln n — roughly 7 around 1,000 and 28 around 10^12 — but single gaps vary a lot. Twin primes such as 101 and 103 differ by only 2.
What is the smallest prime with a given number of digits?
Enter 10^(d − 1) for d digits. The first 4-digit prime is 1,009, the first 7-digit prime is 1,000,003 and the first 13-digit prime is 1,000,000,000,039.
Why stop at 10^24?
Beyond about 3.3 × 10^24 the fixed set of Miller–Rabin bases is no longer proven to be enough, and the tool would only be able to say "probably prime".
Last updated: 2026-10-30