Generate large prime number with specified last digits
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
Generating a large prime with specified last digits is a constrained prime-search problem. The key observation is that if you fix the final decimal suffix, you are no longer searching all large integers. You are searching numbers of the form k * 10^m + suffix, where m is the number of forced trailing digits. Once you express the problem that way, the rest is primality testing and basic modular filtering.
First Check Whether the Suffix Even Allows Primes
Not every suffix can end a prime greater than 5.
If the suffix makes every candidate divisible by 2 or 5, then no large prime exists with that suffix.
Examples of impossible endings for primes greater than 5:
- anything ending in
0 - anything ending in
2 - anything ending in
4 - anything ending in
5 - anything ending in
6 - anything ending in
8
So before writing a search loop, reject impossible suffixes quickly.
You can also filter by divisibility rules for 3, 9, or other small primes when appropriate.
Build Candidates in the Right Shape
If the desired suffix is 37, then every candidate prime must look like:
If the suffix has m digits, the multiplier is 10^m.
In Python:
Now the job becomes: choose prefixes, build candidates, and test primality.
Use Probabilistic Primality Testing
For large integers, you do not want trial division. A probabilistic test such as Miller–Rabin is the usual practical choice.
Here is a simple implementation:
For many engineering tasks, this is a practical answer. For cryptographic production use, you would typically rely on a vetted library rather than handwritten primality code.
Search Strategy
A simple search loop is:
This works because the suffix is fixed and the prefix is random.
Add Small-Prime Filtering for Speed
Before Miller–Rabin, you can quickly reject many candidates using small-prime modular tests.
For example, if a candidate is divisible by 3, 7, 11, or 13, there is no point running the heavier test.
That can speed up the search noticeably, especially when generating many primes.
The pattern is:
- construct candidate
- test divisibility by a small prime list
- only then run Miller–Rabin
Cryptographic Caution
If the prime is for cryptographic use, fixing the last digits reduces randomness. That may or may not be acceptable depending on the protocol and the reason for the suffix requirement.
So ask why the suffix constraint exists.
- if it is for a puzzle or math exercise, fine
- if it is for cryptographic key generation, be very careful
Security-sensitive prime generation should usually use well-reviewed libraries and protocol-specific requirements.
Common Pitfalls
The biggest pitfall is asking for a suffix that makes primality impossible, such as one ending in an even digit or 5.
Another issue is using slow deterministic trial division on very large candidates.
Developers also sometimes forget that fixing a suffix means candidates must be generated as prefix * 10^m + suffix, not by random search with later string filtering.
Finally, for cryptographic primes, handwritten primality code is usually the wrong production choice.
Summary
- Generate candidates as
prefix * 10^m + suffix. - Reject impossible suffixes before searching.
- Use small-prime filtering and then a fast primality test such as Miller–Rabin.
- For large numbers, probabilistic testing is the practical approach.
- If the prime is security-sensitive, use vetted libraries and reconsider whether the suffix constraint is acceptable.
Related reading
- Generate N random numbers within a range with a constant sum
- Generate Non-Degenerate Point Set in 2D - C
- Generate random permutation of huge list in Python
- Generate Unique ID from Alphanumeric String
- Generate new polygons from a cut polygon 2D
- Generate random numbers with a given numerical distribution
- Generating a pseudorandom binary sequence where the same number does not occur more than twice in a row
- Generating a random DAG

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.