Given Prime Number N, Compute the Next Prime?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To compute the next prime after a given prime N, the basic strategy is to test larger candidate numbers until one is prime. For everyday programming tasks, the efficient version is to skip even numbers and test divisibility only up to the square root of each candidate.
The Core Observation
If N is a prime greater than 2, then N is odd. That means the next prime must also be odd, so there is no reason to test even candidates.
The second key observation is about primality testing. To decide whether a candidate x is prime, you do not need to try every divisor from 2 to x - 1. It is enough to check divisors up to sqrt(x).
Why? If x had a factor larger than its square root, the matching paired factor would have to be smaller than the square root. So one of them would already have been found.
A Straightforward Algorithm
The algorithm looks like this:
- if
N < 2, handle that as a special case - set the candidate to
N + 1if needed - if the candidate is even and greater than
2, move to the next odd number - test the candidate for primality
- if it is not prime, add
2and try again
This is simple and effective for moderately sized integers.
Python Implementation
The loop skips every even number after the first candidate, which cuts the search roughly in half immediately.
Why the Square-Root Check Matters
A naive primality test for 101 might try dividing by every integer from 2 to 100. The square-root optimization only checks up to 10, because sqrt(101) is slightly above 10.
That changes the cost of each primality check substantially:
- naive trial division: up to
x - 2checks - optimized trial division: about
sqrt(x) / 2odd checks
For small and medium inputs, this is usually enough.
A C++ Version
This is the same algorithm in a compiled language. It is still based on trial division, just written differently.
When You Need Faster Methods
For very large numbers, repeated square-root trial division becomes too slow. That is where more advanced algorithms help:
- Miller-Rabin for fast probabilistic primality testing
- segmented sieves for finding many primes in a range
- deterministic variants for bounded integer sizes
But if the task is simply “given one prime, find the next prime” for normal integer sizes, the trial-division approach is usually the right first answer.
Common Pitfalls
One mistake is forgetting the special case for 2. It is the only even prime, so generic odd-only logic often needs a small guard for it.
Another issue is checking divisibility all the way to n - 1. That is correct but unnecessarily slow. The square-root bound is the standard optimization.
A third mistake is incrementing by 1 after the first odd candidate instead of by 2. That wastes half the work on even numbers that cannot be prime.
Summary
- To find the next prime after
N, test larger candidates until one is prime. - Skip even candidates once you are above
2. - Test divisibility only up to the square root of the candidate.
- Trial division is simple and practical for ordinary input sizes.
- For very large numbers, switch to faster primality-testing algorithms.
Related reading
- Given some AABBs, find minimum total surface area AABBs that contain them all?
- Given two arrays A and Q, foreach element of of Q, find the element in A with smallest difference
- Given two arrays, find the permutations that give closest distance between two arrays
- Given two lines on a plane, how to find integer points closest to their intersection?
- Go through all permutations of an array recursively
- Good algorithm for combining items from N lists into one with balanced distribution?
- Given two sequences, find the maximal overlap between ending of one and beginning of the other
- Gomoku array-based AI-algorithm?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.