Optimize nextprime by transitioning from 6n system to 30n system #26608
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
References to other Issues or PRs
Brief description of what is fixed or changed
The
nextprime
function has been optimized by transitioning from the 6n system to the 30n system for prime candidate selection. This change reduces the number of unnecessary prime checks, improving the overall performance of the algorithm.Other comments
In the previous implementation, potential primes were located at 6j + {1, 5}. By switching to the 30j + {1, 7, 11, 13, 17, 19, 23, 29} system, we eliminate two additional prime checks per cycle. ( For instance, 30n+5 is also 6n+5. Using this way we skip 30n+5 and 30n+25 ). This adjustment leverages a more efficient pattern for prime candidate selection, leading to faster computations.
Release Notes
nextprime
function by switching from 6n + {1,5} system to 30n + {1, 7, 11, 13, 17, 19, 23, 29} system, reducing unnecessary prime checks and improving performance.