|
| 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