Next Prime Number Calculator
Written by Thierno Sadou Diallo, formula verified per our methodology • Last checked on 9/10/2026
The next prime number after a given number is found by testing each following integer until one is found that is not divisible by any number between 2 and its square root. The next prime number after 20 is 23.
Explanation
Unlike many mathematical properties, there is no closed formula that directly gives "the next prime number" after a given number — the only general method is to test each integer one by one, in increasing order, until one is found that is prime. A number is prime if it is not divisible by any integer between 2 and its square root (no need to test beyond: if a number has a divisor greater than its square root, it necessarily also has a divisor less than it). This calculator applies exactly this method, the same as the one used by our prime number calculator to test the primality of a single number, but repeated in a loop until a match is found. Prime numbers occupy a central place in number theory and cryptography (RSA encryption, for example, relies on the difficulty of factoring the product of two large primes), but also in more recreational contexts, such as searching for recreational patterns or checking arithmetic hypotheses.
Example: search starting from 20
Inputs
Starting number: 20.
Calculation
21 = 3 × 7 (not prime). 22 = 2 × 11 (not prime). 23: no divisor between 2 and √23 ≈ 4.8 (2, 3, 4 do not divide 23) — 23 is prime.
Result
The next prime number after 20 is 23.
Frequently asked questions
Is there a formula to directly calculate the nth prime number?
No, and it is one of the most studied open questions in number theory. There are approximations (such as the prime number theorem, which estimates their density), but no simple closed formula gives exactly the nth prime number or the prime number after a given number — only a search by testing remains guaranteed exact.
Why test only up to the square root of the number?
If a number n has a divisor d greater than its square root, then n ÷ d is necessarily a divisor less than that square root. In other words, any divisor beyond √n necessarily has a "partner" below it — so it is enough to test up to √n to be sure of missing no possible divisor. This same divisor-search principle is also at the heart of our GCD and LCM calculator.
Does this calculator work for very large numbers?
The search stays nearly instant up to several million, but becomes noticeably slower beyond a billion — the trial-division method is not the one used in cryptography for numbers of hundreds of digits, where much faster probabilistic primality tests (such as Miller-Rabin) are needed.