Repository navigation
Primality is decided by Miller-Rabin and Lucas-Lehmer, and finishes - #2232
Merged
Merged
Conversation
Primes.IsPrime was trial division, decided only for a number that fits a long, and it had three defects: - It hung. The loop was divisor * divisor <= value, and past divisor = 3037000499 the square overflows long and wraps negative. So for a prime between 3037000499^2 and 2^63 the loop ran until the divisor reached the number itself. 9223372036854775783 in PP, the largest prime below 2^63, did not finish on 2.6.0 or on master. - It was slow: about 1.5e9 divisions for a prime near 2^63, and 1.1 s for 2^61 - 1. - It left every number past 2^63 open, the Mersenne prime 2^89 - 1 and the composite 2^67 - 1 alike. @Happypig375 pointed to that on #2221. It is now: - Miller-Rabin to the first thirteen primes as bases. That is exact below psi13 = 3317044064679887385961981, the least strong pseudoprime to all thirteen (Sorenson and Webster, "Strong pseudoprimes to twelve prime bases", Math. Comp. 86 (2017)). The first twelve bases already suffice below 2^64. - Lucas-Lehmer for a Mersenne number 2^p - 1, after checking that p is prime, for p up to 5000. - Past psi13 and up to 4096 bits, a failed round still proves a number composite. A number that passes every round is left open, since passing is not a proof. 9223372036854775783 in PP is True in 81 ms, 2^89 - 1 is prime, and 10^30 + 1 is composite. 10^30 + 57, a prime past psi13, stays open, as does psi13 itself. The compiled PP membership calls the same test for any whole double. It was left open past 2^63. #2221's MembershipInASetOperationTest, and its BREAKING-CHANGES entry, used 2^89 - 1 as a number PP leaves open. Lucas-Lehmer now decides it, so both use 10^30 + 57 instead, with an interval that holds it, [0; 10^31], and the entry's values re-measured on 2.6.0 and on this build. PrimalityTest has 16 decided cases under a 30-second cap, among them the largest prime below 2^63, seven Mersenne numbers from 2^61 - 1 to 2^4423 - 1, the least strong pseudoprime to the first twelve bases, Carmichael numbers and base-2 strong pseudoprimes. It has two cases that stay open, every n below 200000 against a sieve, and 2000 random odd n below 10^12 against trial division. Fixes #2230. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2232 +/- ##
==========================================
- Coverage 86.67% 86.67% -0.01%
==========================================
Files 310 310
Lines 67691 67717 +26
Branches 19634 19644 +10
==========================================
+ Hits 58670 58691 +21
- Misses 4905 4910 +5
Partials 4116 4116
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2230. Raised by @Happypig375 on #2221.
Primes.IsPrimewas trial division, decided only for a number that fits along. It had three defects:divisor * divisor <= value. Pastdivisor = 3037000499the square overflowslongand wraps negative, so for a prime between3037000499²and2^63the loop ran until the divisor reached the number itself.1.5 · 10^9divisions for a prime near2^63, and 1.1 s for2^61 - 1.2^63open: the Mersenne prime2^89 - 1and the composite2^67 - 1alike.It is now:
ψ13 = 3317044064679887385961981, the least strong pseudoprime to all thirteen. The bound is from Sorenson and Webster, Strong pseudoprimes to twelve prime bases, Math. Comp. 86 (2017). The first twelve bases already suffice below2^64.2^p - 1, after checking thatpis prime, forpup to 5000.ψ13, up to 4096 bits, a failed round still proves a number composite. A number that passes every round is left open, since passing is not a proof.The compiled
PPmembership from #2223 calls the same test for any whole double; it had been left open past2^63.9223372036854775783 in PPTrue, in 81 ms(2^89 - 1) in PPTrue(10^30 + 1) in PPFalse(10^30 + 57) in PP, a prime pastψ13#2221's
MembershipInASetOperationTestand its BREAKING-CHANGES entry used2^89 - 1as a numberPPleaves open. Lucas–Lehmer now decides it, so both use10^30 + 57instead, with[0; 10^31]as an interval that holds it. The entry's values are re-measured on 2.6.0 and on this build.Tests.
PrimalityTesthas:2^63, seven Mersenne numbers from2^61 - 1to2^4423 - 1, the least strong pseudoprime to the first twelve bases, Carmichael numbers and base-2 strong pseudoprimes;nbelow 200000, checked against a sieve;nbelow10^12, checked against trial division.Full suite 18080/0, interactive trio, AOT smoke 73/0, all frameworks build.
🤖 Generated with Claude Code