Next prime number calculator

Enter a whole number up to 1,000,000,000,000 to find the first prime number that is strictly greater.

Whole number n

Next prime number

101

Difference p - n1
Odd candidates tested1

The first prime strictly greater than 100.Read explanation below

Contents

How the next-prime calculator works

A sequence of numbered positions with the next prime position highlighted

Enter a non-negative whole number n. The calculator returns the first prime p that is strictly greater than n, then shows the gap and how many odd candidates it tested.

What โ€œnextโ€ means here

The input itself is never returned. If n is already prime, the search still begins at n + 1. Thus 17 leads to 19, not 17.

p=minโก{qโˆˆPโˆฃq>n}p = \min \{q \in \mathbb{P} \mid q > n\}

The condition q > n makes the direction unambiguous. This tool does not look for the closest prime on either side; it always moves to the right on the number line.

If no divisor is found in that complete range, the candidate is prime and the search stops. The calculation is exact, with no rounding or probabilistic test.

Checks you can reproduce

Starting at 0. The answer is 2. The gap is 2 and only one candidate is counted. This confirms that 0 and 1 are not treated as primes.

Starting at 14. The algorithm tests 15, rejects it because it is divisible by 3, and then accepts 17. The result is 17, the gap is 3, and two odd candidates were tested.

Starting at 100. The first candidate, 101, is prime. Both the gap and candidate count are 1, so a larger input does not automatically mean a long search.

Starting at 1000. The odd candidates are 1001, 1003, 1005, 1007 and 1009. The first four are composite; 1009 is prime. The result is 1009 with a gap of 9 and five tested candidates.

Limits and result details

The input must be a whole number from 0 through 1,000,000,000,000. A decimal, negative number or blank field is rejected. The result may be slightly above the input limit because that limit applies to n, not to p.

The candidate count is not a count of divisions. A composite number can fail on its first small divisor, while a prime must be checked through the full square-root boundary. Even numbers skipped in advance are not included.

At the upper input limit, the next prime is 1,000,000,000,039. The gap is 39 and 20 odd candidates are tested. Numbers with hundreds of digits need specialised big-integer algorithms; they are outside this calculatorโ€™s scope.

Questions about the next prime

The crucial points are the strict inequality, the one-way search and the difference between candidates and arithmetic operations.

What happens if n is already prime?

The calculator does not return n. It finds the first prime strictly greater than n, so the result after 17 is 19.

Is the next prime the same as the nearest prime?

No. The next prime must be greater than n. The nearest prime may be on the left; for 12 the nearest is 11, while the next is 13.

Why does 1 lead to 2?

Two is the smallest prime and it is strictly greater than 1. One itself has only one positive divisor and is not prime.

Are even numbers counted as candidates?

Only the special value 2 needs separate handling. Every larger even number is composite, so it is skipped and not counted.

Can the result exceed one trillion?

Yes. One trillion is the maximum input. Its next prime is 1,000,000,000,039, which can still be represented exactly.

How can I check the result manually?

Verify that p is greater than n, every integer between them is composite, and p has no divisor up to and including its square root.

Similar calculators

You may find the following calculators on the same topic useful:

Share on social media

If you liked it, please share the calculator on your social media platforms. It`s easy for you and beneficial for the project`s promotion. Thank you!