public static IEnumerable<BigInt> FibonacciSequence(Func<BigInt, bool> stopPredicate) { BigInt fibo1 = new BigInt(1); BigInt fibo2 = new BigInt(1); yield return fibo1; yield return fibo2; while (true) { fibo1 = fibo1 + fibo2; fibo2 = fibo1 + fibo2; if (!stopPredicate(fibo1)) { yield break; } yield return fibo1; if (!stopPredicate(fibo2)) { yield break; } yield return fibo2; } }
public override void Solve() { BigInt number = new BigInt(1); for (int counter = 0; counter < 1000; counter++) { number = number + number; } int sum = number.ToString() .ToCharArray() .Select(c => int.Parse(c.ToString())) .Sum(); Verify(sum); }
private static BigInt MultiplyInternal(BigInt number, long multiplier) { if (multiplier % 10 == 0) { byte factorsOfTen = 0; while (multiplier % 10 == 0) { multiplier /= 10; factorsOfTen++; } number = new BigInt( Enumerable.Repeat<byte>(0, factorsOfTen) .Concat(number.digits) .ToArray()); } if (multiplier == 1) { return number; } else if (multiplier == 0) { return default(BigInt); } byte carry = 0; byte[] resultArray = null; byte product = 0; BigInt result = new BigInt(new byte[0]); var digits = multiplier.Digits(); for (int digitIndex = 0; digitIndex < digits.Length; digitIndex++) { if (digits[digitIndex] == 0) { continue; } resultArray = new byte[number.Length]; carry = 0; product = 0; for (int index = 0; index < number.Length; index++) { product = (byte)(number[index] * digits[digitIndex]); product += carry; if (product > 9) { carry = (byte)(product / 10); product %= 10; } else { carry = 0; } resultArray[index] = product; } if (carry != 0) { resultArray = resultArray.Concat(new byte[] { carry }).ToArray(); } result = result + (new BigInt(resultArray) * 10.Power(digitIndex)); } return result; }
private static BigInt SumInternal(BigInt first, BigInt second) { if (first.Length < second.Length) { BigInt swap = first; first = second; second = swap; } bool carry = false; byte[] result = new byte[first.Length]; byte sumDigits = 0; for (int index = 0; index < first.Length; index++) { if (index < second.Length) { sumDigits = (byte)(first[index] + second[index]); } else { sumDigits = first[index]; } if (carry) { sumDigits++; } if (sumDigits > 9) { sumDigits -= 10; carry = true; } else { carry = false; } result[index] = (byte)sumDigits; } if (carry) { result = result.Concat(new byte[] { 1 }).ToArray(); } return new BigInt(result); }
public static BigInt Factorial(this int number) { BigInt factorer = new BigInt(1); for (int counter = 1; counter <= 100; counter++) { factorer = factorer * counter; } return factorer; }