Implement UInt128 exponentiation and apply in Mersenne scan
This commit is contained in:
1 parent
9eb50bee3b
commit
01277832e6
3 files changed
+63
-9
No files matched your search
@@ -112,6 +112,40 @@ public class UInt128ExtensionsTests
|
||||
mod3.Should().Be(expected3);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(2UL, 0UL)]
|
||||
[InlineData(2UL, 1UL)]
|
||||
[InlineData(2UL, 63UL)]
|
||||
[InlineData(2UL, 127UL)]
|
||||
[InlineData(3UL, 5UL)]
|
||||
public void Pow_matches_BigInteger(ulong value, ulong exponent)
|
||||
{
|
||||
UInt128 v = value;
|
||||
UInt128 expected = (UInt128)BigPow(value, exponent);
|
||||
|
||||
v.Pow(exponent).Should().Be(expected);
|
||||
}
|
||||
|
||||
private static BigInteger BigPow(ulong value, ulong exponent)
|
||||
{
|
||||
BigInteger result = BigInteger.One;
|
||||
BigInteger baseValue = value;
|
||||
|
||||
while (exponent != 0UL)
|
||||
{
|
||||
if ((exponent & 1UL) != 0UL)
|
||||
{
|
||||
result *= baseValue;
|
||||
}
|
||||
|
||||
baseValue *= baseValue;
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(2UL, 10UL, 17UL)]
|
||||
|
||||
Reference in new issue
Block a user