BigInteger shifting mapped to constants for an improved performance.

This commit is contained in:
mlemanczyk committed 2026-01-22 19:01:03 +01:00
1 parent cc8bb98e2c
commit dfd3f71f5d
4 files changed
+670 -9

No files matched your search

+129 -1
View File
@@ -6,10 +6,138 @@ namespace PerfectNumbers.Core
{
public static readonly BigInteger OneShiftedLeft63 = BigInteger.One << 63;
public static readonly BigInteger OneShiftedLeft256MinusOne = (BigInteger.One << 256) - BigInteger.One;
public static readonly BigInteger Two = (BigInteger)2;
public static readonly BigInteger UlongMaxValue = (BigInteger)ulong.MaxValue;
public static readonly BigInteger UlongMaxValuePlusOne = UlongMaxValue + BigInteger.One;
public static readonly BigInteger UInt128MaxValue = (BigInteger)UInt128.MaxValue;
public static readonly BigInteger UInt128MaxValuePlusOne = UInt128MaxValue + BigInteger.One;
public static readonly BigInteger Two = 2UL;
public static readonly BigInteger Pow2Exact2 = 4UL;
public static readonly BigInteger Pow2Exact3 = 8UL;
public static readonly BigInteger Pow2Exact4 = 16UL;
public static readonly BigInteger Pow2Exact5 = 32UL;
public static readonly BigInteger Pow2Exact6 = 64UL;
public static readonly BigInteger Pow2Exact7 = 128UL;
public static readonly BigInteger Pow2Exact8 = 256UL;
public static readonly BigInteger Pow2Exact9 = 512UL;
public static readonly BigInteger Pow2Exact10 = 1_024UL;
public static readonly BigInteger Pow2Exact11 = 2_048UL;
public static readonly BigInteger Pow2Exact12 = 4_096UL;
public static readonly BigInteger Pow2Exact13 = 8_192UL;
public static readonly BigInteger Pow2Exact14 = 16_384UL;
public static readonly BigInteger Pow2Exact15 = 32_768UL;
public static readonly BigInteger Pow2Exact16 = 65_536UL;
public static readonly BigInteger Pow2Exact17 = 131_072UL;
public static readonly BigInteger Pow2Exact18 = 262_144UL;
public static readonly BigInteger Pow2Exact19 = 524_288UL;
public static readonly BigInteger Pow2Exact20 = 1_048_576UL;
public static readonly BigInteger Pow2Exact21 = 2_097_152UL;
public static readonly BigInteger Pow2Exact22 = 4_194_304UL;
public static readonly BigInteger Pow2Exact23 = 8_388_608UL;
public static readonly BigInteger Pow2Exact24 = 16_777_216UL;
public static readonly BigInteger Pow2Exact25 = 33_554_432UL;
public static readonly BigInteger Pow2Exact26 = 67_108_864UL;
public static readonly BigInteger Pow2Exact27 = 134_217_728UL;
public static readonly BigInteger Pow2Exact28 = 268_435_456UL;
public static readonly BigInteger Pow2Exact29 = 536_870_912UL;
public static readonly BigInteger Pow2Exact30 = 1_073_741_824UL;
public static readonly BigInteger Pow2Exact31 = 2_147_483_648UL;
public static readonly BigInteger Pow2Exact32 = 4_294_967_296UL;
public static readonly BigInteger Pow2Exact33 = 8_589_934_592UL;
public static readonly BigInteger Pow2Exact34 = 17_179_869_184UL;
public static readonly BigInteger Pow2Exact35 = 34_359_738_368UL;
public static readonly BigInteger Pow2Exact36 = 68_719_476_736UL;
public static readonly BigInteger Pow2Exact37 = 137_438_953_472UL;
public static readonly BigInteger Pow2Exact38 = 274_877_906_944UL;
public static readonly BigInteger Pow2Exact39 = 549_755_813_888UL;
public static readonly BigInteger Pow2Exact40 = 1_099_511_627_776UL;
public static readonly BigInteger Pow2Exact41 = 2_199_023_255_552UL;
public static readonly BigInteger Pow2Exact42 = 4_398_046_511_104UL;
public static readonly BigInteger Pow2Exact43 = 8_796_093_022_208UL;
public static readonly BigInteger Pow2Exact44 = 17_592_186_044_416UL;
public static readonly BigInteger Pow2Exact45 = 35_184_372_088_832UL;
public static readonly BigInteger Pow2Exact46 = 70_368_744_177_664UL;
public static readonly BigInteger Pow2Exact47 = 140_737_488_355_328UL;
public static readonly BigInteger Pow2Exact48 = 281_474_976_710_656UL;
public static readonly BigInteger Pow2Exact49 = 562_949_953_421_312UL;
public static readonly BigInteger Pow2Exact50 = 1_125_899_906_842_624UL;
public static readonly BigInteger Pow2Exact51 = 2_251_799_813_685_248UL;
public static readonly BigInteger Pow2Exact52 = 4_503_599_627_370_496UL;
public static readonly BigInteger Pow2Exact53 = 9_007_199_254_740_992UL;
public static readonly BigInteger Pow2Exact54 = 18_014_398_509_481_984UL;
public static readonly BigInteger Pow2Exact55 = 36_028_797_018_963_968UL;
public static readonly BigInteger Pow2Exact56 = 72_057_594_037_927_936UL;
public static readonly BigInteger Pow2Exact57 = 144_115_188_075_855_872UL;
public static readonly BigInteger Pow2Exact58 = 288_230_376_151_711_744UL;
public static readonly BigInteger Pow2Exact59 = 576_460_752_303_423_488UL;
public static readonly BigInteger Pow2Exact60 = 1_152_921_504_606_846_976UL;
public static readonly BigInteger Pow2Exact61 = 2_305_843_009_213_693_952UL;
public static readonly BigInteger Pow2Exact62 = 4_611_686_018_427_387_904UL;
public static readonly BigInteger Pow2Exact63 = 9_223_372_036_854_775_808UL;
public static readonly BigInteger Pow2MinusOne1 = 2UL - 1UL;
public static readonly BigInteger Pow2MinusOne2 = 4UL - 1UL;
public static readonly BigInteger Pow2MinusOne3 = 8UL - 1UL;
public static readonly BigInteger Pow2MinusOne4 = 16UL - 1UL;
public static readonly BigInteger Pow2MinusOne5 = 32UL - 1UL;
public static readonly BigInteger Pow2MinusOne6 = 64UL - 1UL;
public static readonly BigInteger Pow2MinusOne7 = 128UL - 1UL;
public static readonly BigInteger Pow2MinusOne8 = 256UL - 1UL;
public static readonly BigInteger Pow2MinusOne9 = 512UL - 1UL;
public static readonly BigInteger Pow2MinusOne10 = 1_024UL - 1UL;
public static readonly BigInteger Pow2MinusOne11 = 2_048UL - 1UL;
public static readonly BigInteger Pow2MinusOne12 = 4_096UL - 1UL;
public static readonly BigInteger Pow2MinusOne13 = 8_192UL - 1UL;
public static readonly BigInteger Pow2MinusOne14 = 16_384UL - 1UL;
public static readonly BigInteger Pow2MinusOne15 = 32_768UL - 1UL;
public static readonly BigInteger Pow2MinusOne16 = 65_536UL - 1UL;
public static readonly BigInteger Pow2MinusOne17 = 131_072UL - 1UL;
public static readonly BigInteger Pow2MinusOne18 = 262_144UL - 1UL;
public static readonly BigInteger Pow2MinusOne19 = 524_288UL - 1UL;
public static readonly BigInteger Pow2MinusOne20 = 1_048_576UL - 1UL;
public static readonly BigInteger Pow2MinusOne21 = 2_097_152UL - 1UL;
public static readonly BigInteger Pow2MinusOne22 = 4_194_304UL - 1UL;
public static readonly BigInteger Pow2MinusOne23 = 8_388_608UL - 1UL;
public static readonly BigInteger Pow2MinusOne24 = 16_777_216UL - 1UL;
public static readonly BigInteger Pow2MinusOne25 = 33_554_432UL - 1UL;
public static readonly BigInteger Pow2MinusOne26 = 67_108_864UL - 1UL;
public static readonly BigInteger Pow2MinusOne27 = 134_217_728UL - 1UL;
public static readonly BigInteger Pow2MinusOne28 = 268_435_456UL - 1UL;
public static readonly BigInteger Pow2MinusOne29 = 536_870_912UL - 1UL;
public static readonly BigInteger Pow2MinusOne30 = 1_073_741_824UL - 1UL;
public static readonly BigInteger Pow2MinusOne31 = 2_147_483_648UL - 1UL;
public static readonly BigInteger Pow2MinusOne32 = 4_294_967_296UL - 1UL;
public static readonly BigInteger Pow2MinusOne33 = 8_589_934_592UL - 1UL;
public static readonly BigInteger Pow2MinusOne34 = 17_179_869_184UL - 1UL;
public static readonly BigInteger Pow2MinusOne35 = 34_359_738_368UL - 1UL;
public static readonly BigInteger Pow2MinusOne36 = 68_719_476_736UL - 1UL;
public static readonly BigInteger Pow2MinusOne37 = 137_438_953_472UL - 1UL;
public static readonly BigInteger Pow2MinusOne38 = 274_877_906_944UL - 1UL;
public static readonly BigInteger Pow2MinusOne39 = 549_755_813_888UL - 1UL;
public static readonly BigInteger Pow2MinusOne40 = 1_099_511_627_776UL - 1UL;
public static readonly BigInteger Pow2MinusOne41 = 2_199_023_255_552UL - 1UL;
public static readonly BigInteger Pow2MinusOne42 = 4_398_046_511_104UL - 1UL;
public static readonly BigInteger Pow2MinusOne43 = 8_796_093_022_208UL - 1UL;
public static readonly BigInteger Pow2MinusOne44 = 17_592_186_044_416UL - 1UL;
public static readonly BigInteger Pow2MinusOne45 = 35_184_372_088_832UL - 1UL;
public static readonly BigInteger Pow2MinusOne46 = 70_368_744_177_664UL - 1UL;
public static readonly BigInteger Pow2MinusOne47 = 140_737_488_355_328UL - 1UL;
public static readonly BigInteger Pow2MinusOne48 = 281_474_976_710_656UL - 1UL;
public static readonly BigInteger Pow2MinusOne49 = 562_949_953_421_312UL - 1UL;
public static readonly BigInteger Pow2MinusOne50 = 1_125_899_906_842_624UL - 1UL;
public static readonly BigInteger Pow2MinusOne51 = 2_251_799_813_685_248UL - 1UL;
public static readonly BigInteger Pow2MinusOne52 = 4_503_599_627_370_496UL - 1UL;
public static readonly BigInteger Pow2MinusOne53 = 9_007_199_254_740_992UL - 1UL;
public static readonly BigInteger Pow2MinusOne54 = 18_014_398_509_481_984UL - 1UL;
public static readonly BigInteger Pow2MinusOne55 = 36_028_797_018_963_968UL - 1UL;
public static readonly BigInteger Pow2MinusOne56 = 72_057_594_037_927_936UL - 1UL;
public static readonly BigInteger Pow2MinusOne57 = 144_115_188_075_855_872UL - 1UL;
public static readonly BigInteger Pow2MinusOne58 = 288_230_376_151_711_744UL - 1UL;
public static readonly BigInteger Pow2MinusOne59 = 576_460_752_303_423_488UL - 1UL;
public static readonly BigInteger Pow2MinusOne60 = 1_152_921_504_606_846_976UL - 1UL;
public static readonly BigInteger Pow2MinusOne61 = 2_305_843_009_213_693_952UL - 1UL;
public static readonly BigInteger Pow2MinusOne62 = 4_611_686_018_427_387_904UL - 1UL;
public static readonly BigInteger Pow2MinusOne63 = 9_223_372_036_854_775_808UL - 1UL;
public static readonly BigInteger Pow2MinusOne1024 = (BigInteger.One << 1024) - BigInteger.One;
}
}
@@ -1113,8 +1113,8 @@ internal static class BitContradictionSolverWithAMultiplier
return;
}
int shift = zeroTailStart + 1 > int.MaxValue ? int.MaxValue : (int)(zeroTailStart + 1);
maxA = (BigInteger.One << shift) - BigInteger.One;
int shift = zeroTailStart + 1L > int.MaxValue ? int.MaxValue : (int)(zeroTailStart + 1L);
maxA = Pow2Provider.BigIntegerMinusOne(shift);
minA = BigInteger.Zero;
if (zeroTailStart == maxAllowedA)
@@ -1122,15 +1122,15 @@ internal static class BitContradictionSolverWithAMultiplier
if (top0KnownOne)
{
shift = maxAllowedA > int.MaxValue ? int.MaxValue : (int)maxAllowedA;
minA = BigInteger.One << shift;
}
else if (top0KnownZero && top1KnownOne && maxAllowedA - 1 >= 0)
else if (top0KnownZero && top1KnownOne && maxAllowedA - 1L >= 0)
{
// zeroTailStart is index from here. We're reusing the variable to limit registry pressure.
zeroTailStart = maxAllowedA - 1;
zeroTailStart = maxAllowedA - 1L;
shift = zeroTailStart > int.MaxValue ? int.MaxValue : (int)zeroTailStart;
minA = BigInteger.One << shift;
}
minA = Pow2Provider.BigInteger(shift);
}
}
@@ -1400,7 +1400,7 @@ internal static class BitContradictionSolverWithAMultiplier
// This is a necessary condition for q | (2^p - 1) when p >= 1024 (true here).
const int ForcedBits = ForcedALowBits;
BigInteger mask = (BigInteger.One << ForcedBits) - BigInteger.One;
BigInteger mask = Pow2Provider.BigIntegerMinusOne(ForcedBits);
// Build q (low 1024 bits) from qOneOffsets. q is small anyway, but do it generically.
BigInteger qMod2k = BigInteger.Zero;
@@ -1408,7 +1408,7 @@ internal static class BitContradictionSolverWithAMultiplier
{
int bit = qOneOffsets[i];
if (bit >= ForcedBits) break;
qMod2k |= (BigInteger.One << bit);
qMod2k |= Pow2Provider.BigInteger(bit);
}
// q must be odd for any Mersenne divisor candidate.
+146
View File
@@ -0,0 +1,146 @@
using System.Numerics;
namespace PerfectNumbers.Core
{
public static class Pow2Provider
{
public static BigInteger BigInteger(int i) => i switch
{
0 => System.Numerics.BigInteger.One,
1 => BigIntegerNumbers.Two,
2 => BigIntegerNumbers.Pow2Exact2,
3 => BigIntegerNumbers.Pow2Exact3,
4 => BigIntegerNumbers.Pow2Exact4,
5 => BigIntegerNumbers.Pow2Exact5,
6 => BigIntegerNumbers.Pow2Exact6,
7 => BigIntegerNumbers.Pow2Exact7,
8 => BigIntegerNumbers.Pow2Exact8,
9 => BigIntegerNumbers.Pow2Exact9,
10 => BigIntegerNumbers.Pow2Exact10,
11 => BigIntegerNumbers.Pow2Exact11,
12 => BigIntegerNumbers.Pow2Exact12,
13 => BigIntegerNumbers.Pow2Exact13,
14 => BigIntegerNumbers.Pow2Exact14,
15 => BigIntegerNumbers.Pow2Exact15,
16 => BigIntegerNumbers.Pow2Exact16,
17 => BigIntegerNumbers.Pow2Exact17,
18 => BigIntegerNumbers.Pow2Exact18,
19 => BigIntegerNumbers.Pow2Exact19,
20 => BigIntegerNumbers.Pow2Exact20,
21 => BigIntegerNumbers.Pow2Exact21,
22 => BigIntegerNumbers.Pow2Exact22,
23 => BigIntegerNumbers.Pow2Exact23,
24 => BigIntegerNumbers.Pow2Exact24,
25 => BigIntegerNumbers.Pow2Exact25,
26 => BigIntegerNumbers.Pow2Exact26,
27 => BigIntegerNumbers.Pow2Exact27,
28 => BigIntegerNumbers.Pow2Exact28,
29 => BigIntegerNumbers.Pow2Exact29,
30 => BigIntegerNumbers.Pow2Exact30,
31 => BigIntegerNumbers.Pow2Exact31,
32 => BigIntegerNumbers.Pow2Exact32,
33 => BigIntegerNumbers.Pow2Exact33,
34 => BigIntegerNumbers.Pow2Exact34,
35 => BigIntegerNumbers.Pow2Exact35,
36 => BigIntegerNumbers.Pow2Exact36,
37 => BigIntegerNumbers.Pow2Exact37,
38 => BigIntegerNumbers.Pow2Exact38,
39 => BigIntegerNumbers.Pow2Exact39,
40 => BigIntegerNumbers.Pow2Exact40,
41 => BigIntegerNumbers.Pow2Exact41,
42 => BigIntegerNumbers.Pow2Exact42,
43 => BigIntegerNumbers.Pow2Exact43,
44 => BigIntegerNumbers.Pow2Exact44,
45 => BigIntegerNumbers.Pow2Exact45,
46 => BigIntegerNumbers.Pow2Exact46,
47 => BigIntegerNumbers.Pow2Exact47,
48 => BigIntegerNumbers.Pow2Exact48,
49 => BigIntegerNumbers.Pow2Exact49,
50 => BigIntegerNumbers.Pow2Exact50,
51 => BigIntegerNumbers.Pow2Exact51,
52 => BigIntegerNumbers.Pow2Exact52,
53 => BigIntegerNumbers.Pow2Exact53,
54 => BigIntegerNumbers.Pow2Exact54,
55 => BigIntegerNumbers.Pow2Exact55,
56 => BigIntegerNumbers.Pow2Exact56,
57 => BigIntegerNumbers.Pow2Exact57,
58 => BigIntegerNumbers.Pow2Exact58,
59 => BigIntegerNumbers.Pow2Exact59,
60 => BigIntegerNumbers.Pow2Exact60,
61 => BigIntegerNumbers.Pow2Exact61,
62 => BigIntegerNumbers.Pow2Exact62,
63 => BigIntegerNumbers.Pow2Exact63,
_ => throw new ArgumentOutOfRangeException(nameof(i), $"Parameter value {i} is incorrect"),
};
public static BigInteger BigIntegerMinusOne(int i) => i switch
{
0 => System.Numerics.BigInteger.Zero,
1 => BigIntegerNumbers.Pow2MinusOne1,
2 => BigIntegerNumbers.Pow2MinusOne2,
3 => BigIntegerNumbers.Pow2MinusOne3,
4 => BigIntegerNumbers.Pow2MinusOne4,
5 => BigIntegerNumbers.Pow2MinusOne5,
6 => BigIntegerNumbers.Pow2MinusOne6,
7 => BigIntegerNumbers.Pow2MinusOne7,
8 => BigIntegerNumbers.Pow2MinusOne8,
9 => BigIntegerNumbers.Pow2MinusOne9,
10 => BigIntegerNumbers.Pow2MinusOne10,
11 => BigIntegerNumbers.Pow2MinusOne11,
12 => BigIntegerNumbers.Pow2MinusOne12,
13 => BigIntegerNumbers.Pow2MinusOne13,
14 => BigIntegerNumbers.Pow2MinusOne14,
15 => BigIntegerNumbers.Pow2MinusOne15,
16 => BigIntegerNumbers.Pow2MinusOne16,
17 => BigIntegerNumbers.Pow2MinusOne17,
18 => BigIntegerNumbers.Pow2MinusOne18,
19 => BigIntegerNumbers.Pow2MinusOne19,
20 => BigIntegerNumbers.Pow2MinusOne20,
21 => BigIntegerNumbers.Pow2MinusOne21,
22 => BigIntegerNumbers.Pow2MinusOne22,
23 => BigIntegerNumbers.Pow2MinusOne23,
24 => BigIntegerNumbers.Pow2MinusOne24,
25 => BigIntegerNumbers.Pow2MinusOne25,
26 => BigIntegerNumbers.Pow2MinusOne26,
27 => BigIntegerNumbers.Pow2MinusOne27,
28 => BigIntegerNumbers.Pow2MinusOne28,
29 => BigIntegerNumbers.Pow2MinusOne29,
30 => BigIntegerNumbers.Pow2MinusOne30,
31 => BigIntegerNumbers.Pow2MinusOne31,
32 => BigIntegerNumbers.Pow2MinusOne32,
33 => BigIntegerNumbers.Pow2MinusOne33,
34 => BigIntegerNumbers.Pow2MinusOne34,
35 => BigIntegerNumbers.Pow2MinusOne35,
36 => BigIntegerNumbers.Pow2MinusOne36,
37 => BigIntegerNumbers.Pow2MinusOne37,
38 => BigIntegerNumbers.Pow2MinusOne38,
39 => BigIntegerNumbers.Pow2MinusOne39,
40 => BigIntegerNumbers.Pow2MinusOne40,
41 => BigIntegerNumbers.Pow2MinusOne41,
42 => BigIntegerNumbers.Pow2MinusOne42,
43 => BigIntegerNumbers.Pow2MinusOne43,
44 => BigIntegerNumbers.Pow2MinusOne44,
45 => BigIntegerNumbers.Pow2MinusOne45,
46 => BigIntegerNumbers.Pow2MinusOne46,
47 => BigIntegerNumbers.Pow2MinusOne47,
48 => BigIntegerNumbers.Pow2MinusOne48,
49 => BigIntegerNumbers.Pow2MinusOne49,
50 => BigIntegerNumbers.Pow2MinusOne50,
51 => BigIntegerNumbers.Pow2MinusOne51,
52 => BigIntegerNumbers.Pow2MinusOne52,
53 => BigIntegerNumbers.Pow2MinusOne53,
54 => BigIntegerNumbers.Pow2MinusOne54,
55 => BigIntegerNumbers.Pow2MinusOne55,
56 => BigIntegerNumbers.Pow2MinusOne56,
57 => BigIntegerNumbers.Pow2MinusOne57,
58 => BigIntegerNumbers.Pow2MinusOne58,
59 => BigIntegerNumbers.Pow2MinusOne59,
60 => BigIntegerNumbers.Pow2MinusOne60,
61 => BigIntegerNumbers.Pow2MinusOne61,
62 => BigIntegerNumbers.Pow2MinusOne62,
63 => BigIntegerNumbers.Pow2MinusOne63,
1024 => BigIntegerNumbers.Pow2MinusOne1024,
_ => throw new ArgumentOutOfRangeException(nameof(i), $"Parameter value {i} is incorrect"),
};
}
}