Skip to content

Commit 56e0946

Browse files
Primality is decided by Miller-Rabin and Lucas-Lehmer, and finishes (#2232)
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>
1 parent a4b7b74 commit 56e0946

7 files changed

Lines changed: 210 additions & 40 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ carried it once combined: a second derivative of `1/sqrt(x^2 + 13)` stated `not
121121
| `"(a provided b > 0) + (c provided b > 0)".ToEntity().InnerSimplified` | `a + c provided b > 0 and b > 0` | `a + c provided b > 0` |
122122
| `MathS.Equations("sqrt(y) - x", "y - a").Solve("x", "y")` | `[[sqrt(a), (sqrt(a)) ^ 2], [-sqrt(a), (-sqrt(a)) ^ 2]]` | `[[sqrt(a) provided C(sqrt(a)), (sqrt(a)) ^ 2 provided C(sqrt(a))], [-sqrt(a) provided C(-sqrt(a)), (-sqrt(a)) ^ 2 provided C(-sqrt(a))]]`, `C(v)` being `v = 0 or -1/2 * pi < arg(v) and arg(v) <= 1/2 * pi` |
123123

124+
### Primality is decided by Miller–Rabin and Lucas–Lehmer, and finishes
125+
126+
**Silent: a membership in `PP` left open past the machine word is decided where a test proves it, and one that did
127+
not finish does.** Primality was trial division, exact only below `2^63`. For a prime past `3037000499^2`, just below
128+
`2^63`, the square of the divisor overflowed and the division never finished, and a prime near `2^63` took about
129+
`1.5e9` divisions. It is now Miller–Rabin to the first thirteen primes as bases, exact below
130+
`3317044064679887385961981` (Sorenson and Webster, "Strong pseudoprimes to twelve prime bases", Math. Comp. 86
131+
(2017)), and Lucas–Lehmer for a Mersenne number `2^p - 1`. Past those a failed round still proves a number composite,
132+
and a number that passes every round is left open, since passing is not a proof
133+
([#2230](https://github.com/asc-community/AngouriMath/issues/2230)).
134+
135+
| Input | Was (2.6.0) | Now |
136+
|---|---|---|
137+
| `"9223372036854775783 in PP".ToEntity().Evaled` | does not finish; stopped after 60 s | `True`, in 81 ms |
138+
| `"(2^89 - 1) in PP".ToEntity().Evaled` | `618970019642690137449562111 in PP` | `True` |
139+
| `"(10^30 + 1) in PP".ToEntity().Evaled` | `1000000000000000000000000000001 in PP` | `False` |
140+
| `"(10^30 + 57) in PP".ToEntity().Evaled` | `1000000000000000000000000000057 in PP` | `1000000000000000000000000000057 in PP` |
141+
124142
### A system with a product equation is solved branch by branch
125143

126144
**Silent: solutions that were missing are answered.** `EquationSystem.Solve` eliminated `x * y = 0` for `y` as `0`,
@@ -603,16 +621,16 @@ around a principal `n`-th root of an `n`-th power, for a whole `n >= 2`, establi
603621

604622
**Silent: a membership answered `False` is left open, where it was wrong; and some left open are decided.** Membership
605623
in a union, an intersection and a set difference is decided from each side's by Kleene's connectives. A union was
606-
`False` where one side left membership open and the other excluded the element: `2^89 - 1` is a Mersenne prime past
607-
the machine word, `PP` leaves its membership open, and it was outside `PP \/ [0; 1]`. An intersection and a difference
624+
`False` where one side left membership open and the other excluded the element: `10^30 + 57` is a prime past what the
625+
prime test proves, `PP` leaves its membership open, and it was outside `PP \/ [0; 1]`. An intersection and a difference
608626
were decided only where both sides were, and are now decided where one side settles it
609627
([#2219](https://github.com/asc-community/AngouriMath/issues/2219)).
610628

611629
| Input | Was (2.6.0) | Now |
612630
|---|---|---|
613-
| `"(2^89 - 1) in (PP \/ [0; 1])".ToEntity().Evaled` | `False` | `618970019642690137449562111 in PP \/ [0; 1]` |
614-
| `"(2^89 - 1) in ([0; 1] \ PP)".ToEntity().Evaled` | `618970019642690137449562111 in [0; 1] \ PP` | `False` |
615-
| `"(2^89 - 1) in (PP \ [0; 10^30])".ToEntity().Evaled` | `618970019642690137449562111 in PP \ [0; 1000000000000000000000000000000]` | `False` |
631+
| `"(10^30 + 57) in (PP \/ [0; 1])".ToEntity().Evaled` | `False` | `1000000000000000000000000000057 in PP \/ [0; 1]` |
632+
| `"(10^30 + 57) in ([0; 1] \ PP)".ToEntity().Evaled` | `1000000000000000000000000000057 in [0; 1] \ PP` | `False` |
633+
| `"(10^30 + 57) in (PP \ [0; 10^31])".ToEntity().Evaled` | `1000000000000000000000000000057 in PP \ [0; 10000000000000000000000000000000]` | `False` |
616634

617635
### A system is not answered with a copy of one of its rows
618636

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
478478
[Tests/UnitTests/Core/Sets/MembershipInASetOperationTest.cs]
479479
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
480480

481+
[Tests/UnitTests/Core/Sets/PrimalityTest.cs]
482+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
483+
481484
[Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs]
482485
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
483486

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ public override bool TryContains(Entity entity, out bool contains)
761761
}
762762

763763
/// <summary>
764-
/// Whether the whole number is prime, or <see langword="null"/> where it does
765-
/// not fit a machine word and trial division is not attempted.
764+
/// Whether the whole number is prime, or <see langword="null"/> where it is not
765+
/// decided: see <see cref="Functions.Primes.IsPrime"/>.
766766
/// </summary>
767767
internal static bool? IsDecidablyPrime(Integer whole)
768768
=> Functions.Primes.IsPrime(whole.EInteger);

‎Sources/AngouriMath/Functions/NumberTheory/Primes.cs‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,88 @@ static void AddPrimeI()
105105
}
106106
}
107107

108+
// The first thirteen primes. Miller–Rabin to them as bases is exact below psi13, the least strong
109+
// pseudoprime to all thirteen: Sorenson and Webster, "Strong pseudoprimes to twelve prime bases",
110+
// Math. Comp. 86 (2017). The first twelve already suffice below 2^64.
111+
[ConstantField] private static readonly int[] witnesses = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 };
112+
[ConstantField] private static readonly EInteger psi13 = EInteger.FromString("3317044064679887385961981");
113+
114+
// Past these sizes neither test is attempted, and primality is left open: Lucas–Lehmer at 2^5000 - 1
115+
// takes about 5000 squarings of 5000 bits, and Miller–Rabin at 4096 bits thirteen powers of that size.
116+
private const long LargestMersenneExponent = 5000;
117+
private const long LargestTestedBits = 4096;
118+
108119
/// <summary>
109-
/// Whether the whole number is prime, or <see langword="null"/> where it is not decided:
110-
/// trial division, exact for a number that fits a machine word, and nothing claimed past
111-
/// that, so that a membership in <c>PP</c> is left open rather than answered slowly or
112-
/// wrongly. https://github.com/asc-community/AngouriMath/issues/1450
120+
/// Whether the whole number is prime, or <see langword="null"/> where it is not decided. Miller–Rabin to the
121+
/// first thirteen primes is exact below <c>3317044064679887385961981</c>; past it a witness still proves a
122+
/// number composite, a Mersenne number <c>2^p - 1</c> is decided by Lucas–Lehmer, and a number that passes is
123+
/// left open, since passing is not a proof. This was trial division, exact only below 2^63, about 1.5e9
124+
/// divisions for a prime near it, and for one past 3037000499^2 never finishing, the square of the divisor
125+
/// overflowing. https://github.com/asc-community/AngouriMath/issues/2230
113126
/// </summary>
114127
internal static bool? IsPrime(EInteger n)
115128
{
116129
if (n.CompareTo(EInteger.FromInt32(2)) < 0)
117130
return false;
118-
if (!n.CanFitInInt64())
119-
return null;
120-
var value = n.ToInt64Checked();
121-
if (value < 4)
131+
foreach (var witness in witnesses)
132+
{
133+
if (n.CompareTo(EInteger.FromInt32(witness)) == 0)
134+
return true;
135+
if (n.Remainder(EInteger.FromInt32(witness)).IsZero)
136+
return false;
137+
}
138+
// No factor up to 41 and below 43^2: prime.
139+
if (n.CompareTo(EInteger.FromInt32(43 * 43)) < 0)
122140
return true;
123-
if (value % 2 == 0)
141+
var bits = n.GetUnsignedBitLengthAsInt64();
142+
// 2^p - 1, whose successor is a power of two.
143+
if (n.Add(EInteger.One).And(n).IsZero && bits <= LargestMersenneExponent)
144+
return IsPrime(EInteger.FromInt64(bits)) is true && LucasLehmer(n, bits);
145+
if (bits > LargestTestedBits)
146+
return null;
147+
if (!IsStrongProbablePrime(n))
124148
return false;
125-
for (long divisor = 3; divisor * divisor <= value; divisor += 2)
126-
if (value % divisor == 0)
149+
return n.CompareTo(psi13) < 0 ? true : null;
150+
}
151+
152+
// Whether n passes Miller–Rabin to every witness, n odd and past them.
153+
private static bool IsStrongProbablePrime(EInteger n)
154+
{
155+
var less = n.Subtract(EInteger.One);
156+
var odd = less;
157+
var twos = 0;
158+
while (odd.IsEven)
159+
{
160+
odd = odd.ShiftRight(1);
161+
twos++;
162+
}
163+
foreach (var witness in witnesses)
164+
{
165+
var x = EInteger.FromInt32(witness).ModPow(odd, n);
166+
if (x.Equals(EInteger.One) || x.Equals(less))
167+
continue;
168+
var reached = false;
169+
for (var i = 1; i < twos && !reached; i++)
170+
{
171+
x = x.Multiply(x).Remainder(n);
172+
reached = x.Equals(less);
173+
}
174+
if (!reached)
127175
return false;
176+
}
128177
return true;
129178
}
130179

180+
// 2^p - 1, p an odd prime, is prime exactly when s reaches 0 modulo it after p - 2 steps of s -> s^2 - 2
181+
// from 4.
182+
private static bool LucasLehmer(EInteger mersenne, long exponent)
183+
{
184+
var s = EInteger.FromInt32(4);
185+
for (long i = 0; i < exponent - 2; i++)
186+
s = s.Multiply(s).Subtract(EInteger.FromInt32(2)).Mod(mersenne);
187+
return s.IsZero;
188+
}
189+
131190
/// <summary>
132191
/// The first prime at or after <paramref name="atLeast"/>, or <see langword="null"/>
133192
/// where the search would leave the machine word or run past the steps allowed.

‎Sources/AngouriMath/Numerics/NumberStatements.cs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace AngouriMath.Numerics
1515
/// <summary>
1616
/// Membership in the sets of numbers and divisibility, for the compilers, on the terms evaluation decides
1717
/// them: no truth value at <c>NaN</c>, an infinity in <c>RR</c> and <c>CC</c> as evaluation has it and in
18-
/// none of the sets of whole numbers, and primality left open past the machine word, where the interpreter
19-
/// leaves it. Divisibility is a statement about whole numbers, and has no truth value at any other.
18+
/// none of the sets of whole numbers, and primality by the interpreter's own test. Divisibility is a statement about whole numbers, and has no truth value at any other.
2019
/// https://github.com/asc-community/AngouriMath/issues/2195
2120
/// </summary>
2221
internal static class NumberStatements
@@ -40,10 +39,6 @@ internal enum NumberSet { Complexes, Reals, Integers, PositiveIntegers, NonNegat
4039
_ => null
4140
};
4241

43-
// 2^63: a whole double below it is a long, and the trial division the interpreter decides primality by
44-
// takes it.
45-
private const double PastTheWord = 9223372036854775808.0;
46-
4742
/// <summary>Whether <paramref name="z"/> is in the set of numbers <paramref name="set"/>, a <see cref="NumberSet"/>.</summary>
4843
public static bool? In(NumericsComplex z, int set)
4944
{
@@ -65,7 +60,7 @@ internal enum NumberSet { Complexes, Reals, Integers, PositiveIntegers, NonNegat
6560
NumberSet.Integers => true,
6661
NumberSet.PositiveIntegers => z.Real > 0,
6762
NumberSet.NonNegativeIntegers => z.Real >= 0,
68-
_ => z.Real < 2 ? false : z.Real >= PastTheWord ? null : Primes.IsPrime(EInteger.FromInt64((long)z.Real))
63+
_ => z.Real < 2 ? false : Primes.IsPrime(EFloat.FromDouble(z.Real).ToEInteger())
6964
};
7065
}
7166

‎Sources/Tests/UnitTests/Core/Sets/MembershipInASetOperationTest.cs‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@ namespace AngouriMath.Tests.Core.Sets
1414
{
1515
/// <summary>
1616
/// Membership in a union, an intersection and a difference of sets is decided from each side's by Kleene's
17-
/// connectives, where a side may leave it open: <c>2^89 - 1</c>, a Mersenne prime, is past the machine word
18-
/// and its membership in <c>PP</c> is left open. A union of <c>PP</c> with a set excluding it was
19-
/// <c>False</c>. <a href="https://github.com/asc-community/AngouriMath/issues/2219">#2219</a>
17+
/// connectives, where a side may leave it open: <c>10^30 + 57</c>, a prime past what the prime test proves, has
18+
/// its membership in <c>PP</c> left open. A union of <c>PP</c> with a set excluding it was <c>False</c>. <a href="https://github.com/asc-community/AngouriMath/issues/2219">#2219</a>
2019
/// </summary>
2120
[Trait("Area", "Core")]
2221
public sealed class MembershipInASetOperationTest
2322
{
2423
[Theory]
2524
// One side open, the other excluding it: open, in either order.
26-
[InlineData("(2^89 - 1) in (PP \\/ [0; 1])")]
27-
[InlineData("(2^89 - 1) in ([0; 1] \\/ PP)")]
28-
[InlineData("(2^89 - 1) in (PP \\/ (-oo; 0))")]
29-
[InlineData("(2^89 - 1) in (PP \\/ ({2} \\/ [5; 7]))")]
25+
[InlineData("(10^30 + 57) in (PP \\/ [0; 1])")]
26+
[InlineData("(10^30 + 57) in ([0; 1] \\/ PP)")]
27+
[InlineData("(10^30 + 57) in (PP \\/ (-oo; 0))")]
28+
[InlineData("(10^30 + 57) in (PP \\/ ({2} \\/ [5; 7]))")]
3029
// One side open, the other containing it: open for an intersection, and for a difference whose right side is open.
31-
[InlineData("(2^89 - 1) in (PP /\\ [0; 10^30])")]
32-
[InlineData("(2^89 - 1) in ([0; 10^30] \\ PP)")]
33-
[InlineData("(2^89 - 1) in (PP \\ [0; 1])")]
30+
[InlineData("(10^30 + 57) in (PP /\\ [0; 10^31])")]
31+
[InlineData("(10^30 + 57) in ([0; 10^31] \\ PP)")]
32+
[InlineData("(10^30 + 57) in (PP \\ [0; 1])")]
3433
public void LeftOpen(string input)
3534
=> Assert.IsType<Set.Inf>(input.ToEntity().Evaled);
3635

3736
[Theory]
3837
// A union holds where either side does.
39-
[InlineData("(2^89 - 1) in (PP \\/ [0; 10^30])", "True")]
38+
[InlineData("(10^30 + 57) in (PP \\/ [0; 10^31])", "True")]
4039
[InlineData("2 in (PP \\/ [5; 7])", "True")]
4140
[InlineData("6 in (PP \\/ [5; 7])", "True")]
4241
[InlineData("4 in (PP \\/ [5; 7])", "False")]
4342
// An intersection fails where either side does.
44-
[InlineData("(2^89 - 1) in (PP /\\ (-oo; 0))", "False")]
45-
[InlineData("(2^89 - 1) in (PP /\\ [0; 1])", "False")]
43+
[InlineData("(10^30 + 57) in (PP /\\ (-oo; 0))", "False")]
44+
[InlineData("(10^30 + 57) in (PP /\\ [0; 1])", "False")]
4645
[InlineData("7 in (PP /\\ [5; 10^30])", "True")]
4746
// A difference fails where its left side does, or its right side holds.
48-
[InlineData("(2^89 - 1) in ([0; 1] \\ PP)", "False")]
49-
[InlineData("(2^89 - 1) in (PP \\ [0; 10^30])", "False")]
47+
[InlineData("(10^30 + 57) in ([0; 1] \\ PP)", "False")]
48+
[InlineData("(10^30 + 57) in (PP \\ [0; 10^31])", "False")]
5049
[InlineData("7 in (PP \\ [0; 5])", "True")]
5150
public void Decided(string input, string expected)
5251
=> Assert.Equal(expected.ToEntity(), input.ToEntity().Evaled);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
using AngouriMath;
12+
using AngouriMath.Extensions;
13+
using PeterO.Numbers;
14+
using Xunit;
15+
using static AngouriMath.Entity;
16+
17+
namespace AngouriMath.Tests.Core.Sets
18+
{
19+
/// <summary>
20+
/// Primality by Miller–Rabin to the first thirteen primes, exact below <c>3317044064679887385961981</c> (Sorenson and
21+
/// Webster, Math. Comp. 86 (2017)), and by Lucas–Lehmer for a Mersenne number; past both a witness still proves a
22+
/// number composite and a number that passes is left open. It was trial division, which for a prime just below
23+
/// <c>2^63</c> never finished, the square of the divisor overflowing.
24+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2230">#2230</a>
25+
/// </summary>
26+
[Trait("Area", "Core")]
27+
public sealed class PrimalityTest
28+
{
29+
[Theory]
30+
// The largest prime below 2^63, where trial division did not finish.
31+
[InlineData("9223372036854775783 in PP", "True")]
32+
[InlineData("9223372036854775781 in PP", "False")]
33+
[InlineData("(2^61 - 1) in PP", "True")]
34+
// Mersenne numbers, by Lucas–Lehmer: 2^89 - 1 and 2^127 - 1 are prime, 2^67 - 1 and 2^101 - 1 are not.
35+
[InlineData("(2^89 - 1) in PP", "True")]
36+
[InlineData("(2^127 - 1) in PP", "True")]
37+
[InlineData("(2^67 - 1) in PP", "False")]
38+
[InlineData("(2^101 - 1) in PP", "False")]
39+
// 2^4423 - 1 is prime, and 2^4421 - 1, with a prime exponent, is not.
40+
[InlineData("(2^4423 - 1) in PP", "True")]
41+
[InlineData("(2^4421 - 1) in PP", "False")]
42+
// The least strong pseudoprime to the first twelve prime bases, which 41 exposes.
43+
[InlineData("318665857834031151167461 in PP", "False")]
44+
// Carmichael numbers and strong pseudoprimes to base 2.
45+
[InlineData("561 in PP", "False")]
46+
[InlineData("41041 in PP", "False")]
47+
[InlineData("2047 in PP", "False")]
48+
[InlineData("3215031751 in PP", "False")]
49+
// Past the exact range a witness still proves a number composite.
50+
[InlineData("(10^30 + 1) in PP", "False")]
51+
[InlineData("(10^30 + 3) in PP", "False")]
52+
public void Decided(string input, string expected)
53+
{
54+
var task = Task.Run(() => input.ToEntity().Evaled);
55+
Assert.True(task.Wait(TimeSpan.FromSeconds(30)), $"{input} did not finish");
56+
Assert.Equal(expected.ToEntity(), task.Result);
57+
}
58+
59+
/// <summary>
60+
/// Past the exact range, a number that passes every round is left open: <c>10^30 + 57</c> is prime, and
61+
/// <c>3317044064679887385961981</c>, the least strong pseudoprime to all thirteen bases, is not.
62+
/// </summary>
63+
[Theory]
64+
[InlineData("(10^30 + 57) in PP")]
65+
[InlineData("3317044064679887385961981 in PP")]
66+
public void LeftOpen(string input)
67+
=> Assert.IsType<Set.Inf>(input.ToEntity().Evaled);
68+
69+
[Fact]
70+
public void BelowTwoHundredThousandItIsTheSieve()
71+
{
72+
const int limit = 200000;
73+
var composite = new bool[limit];
74+
for (var i = 2; i * i < limit; i++)
75+
if (!composite[i])
76+
for (var j = i * i; j < limit; j += i)
77+
composite[j] = true;
78+
for (var n = 0; n < limit; n++)
79+
Assert.True(AngouriMath.Functions.Primes.IsPrime(EInteger.FromInt32(n)) == (n >= 2 && !composite[n]), $"{n}");
80+
}
81+
82+
[Fact]
83+
public void BelowTenToTheTwelveItIsTrialDivision()
84+
{
85+
var random = new Random(2230);
86+
for (var t = 0; t < 2000; t++)
87+
{
88+
var n = (long)(random.NextDouble() * 1e12) | 1;
89+
var prime = n >= 2;
90+
for (long d = 3; d * d <= n && prime; d += 2)
91+
prime = n % d != 0;
92+
Assert.True(AngouriMath.Functions.Primes.IsPrime(EInteger.FromInt64(n)) == prime, $"{n}");
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)