private static BigInteger CacheableLinear(long n) { CacheableBigInteger result = 1; for (long i = 2; i <= n; i++) { result.Multiply(i); } return(result); }
public void OperationsTest() { BigInteger initial = BigInteger.Parse("4237894523145135423652643587459827"); BigInteger result0 = initial * 5 * 2 * 10 * 100; BigInteger result1 = result0 + 50 + 100; BigInteger result2 = result1 / 10; BigInteger result3 = result2 - 5; BigInteger result4 = result3 * 10 * 1000000; BigInteger result5 = result4 - 1; BigInteger result6 = result5 / 100; var cacheable = new CacheableBigInteger(initial); cacheable.Multiply(5); cacheable.Multiply(2); cacheable.Multiply(10); cacheable.Multiply(100); cacheable.Add(50); cacheable.Add(100); cacheable.Divide(10); cacheable.Subtract(5); cacheable.Multiply(10); cacheable.Multiply(BigInteger.Parse("1000000")); cacheable.Subtract(BigInteger.Parse("1")); cacheable.Divide(BigInteger.Parse("100")); Assert.AreEqual(result6, cacheable.Value); }
public void OverflowingTest() { var cacheable = new CacheableBigInteger(0); cacheable.Add(long.MaxValue); cacheable.Add(1); Assert.AreEqual(new BigInteger(long.MaxValue) + 1, cacheable.Value); cacheable.Value = new BigInteger(long.MaxValue) + 4; cacheable.Subtract(long.MaxValue); cacheable.Subtract(4); Assert.AreEqual(BigInteger.Zero, cacheable.Value); cacheable.Value = 1; cacheable.Multiply(long.MaxValue); cacheable.Multiply(2); Assert.AreEqual(new BigInteger(long.MaxValue) * 2, cacheable.Value); cacheable.Divide(long.MaxValue); cacheable.Divide(2); Assert.AreEqual(BigInteger.One, cacheable.Value); }
private static BigInteger CacheableOptimized(long n) { CacheableBigInteger result = 1; long currentFactor = n; long currentIncrement = currentFactor - 2; long bound = n / 2; for (long i = 1; i <= bound; i++) { result.Multiply(currentFactor); currentFactor += currentIncrement; currentIncrement -= 2; } if (n % 2 == 1) { result.Multiply(n / 2 + 1); } return(result); }