/// <summary> /// Add two ReallyBigInts together /// </summary> /// <param name="number">A number</param> /// <returns>The sum</returns> public ReallyBigInt Addition(ReallyBigInt number) { string sumString = null; string number1 = number.value; string number2 = this.value; int carryover = 0; // make the string the same size if (number1.Length > number2.Length) { number2 = Zeros(number1.Length - number2.Length) + number2; } if (number2.Length > number1.Length) { number1 = Zeros(number2.Length - number1.Length) + number1; } for (int index = 0; index < number1.Length; index++) { int position = (number1.Length - 1) - index; // working from left to right int digit1 = int.Parse(number1.Substring(position, 1)); int digit2 = int.Parse(number2.Substring(position, 1)); int sumOfDigits = carryover + digit1 + digit2; sumString = (sumOfDigits % 10).ToString() + sumString; // add the digit to the resulting sum carryover = sumOfDigits / 10; // make sure to account for numbers that carry over } if (carryover != 0) { sumString = carryover.ToString() + sumString; } return(new ReallyBigInt(sumString)); }
/// <summary> /// Compare this number to another /// </summary> /// <param name="number">The number to compare</param> /// <returns> -1 if less than, 0 if equal, and 1 if greater</returns> public int Compare(ReallyBigInt number) { int result = 0; string string1 = value; string string2 = number.value; if (string1.Length > string2.Length) { string2 = Zeros(string1.Length - string2.Length) + string2; } else { string1 = Zeros(string2.Length - string1.Length) + string1; } for (int index = 0; index < string1.Length; index++) { int number1 = int.Parse(string1.Substring(index, 1)); int number2 = int.Parse(string2.Substring(index, 1)); if (number1 < number2) { result = -1; break; } if (number1 > number2) { result = 1; break; } } return(result); }
public bool LessThanOrEqual(ReallyBigInt number) { if (Compare(number) <= 0) { return(true); } return(false); }
/// <summary> /// Calculate the factorial up to the given number /// </summary> /// <param name="number">The number</param> /// <returns>The factorial</returns> public static ReallyBigInt BigFactorial(long number) { ReallyBigInt product = new ReallyBigInt("1"); for (long index = 1; index <= number; index++) { product = product.Multiplication(new ReallyBigInt(index.ToString())); } return(product); }
/// <summary> /// This method returns the sum of the digits for a given number. /// </summary> /// <param name="number">The number who digits are being summed</param> /// <returns>The sum of the digits</returns> public static long SumDigits(ReallyBigInt number) { long result = 0; for (int index = 0; index < number.value.Length; index++) { result += long.Parse(number.value.Substring(index, 1)); } return(result); }
/// <summary> /// Return the sum of a series of really big numbers /// </summary> /// <param name="numbers">the numbers</param> /// <returns>the sum</returns> public static ReallyBigInt SeriesSum(List <ReallyBigInt> numbers) { ReallyBigInt result = new ReallyBigInt("0"); foreach (ReallyBigInt number in numbers) { result = result.Addition(number); } return(result); }
/// <summary> /// Raise a ReallyBitInt to a power /// </summary> /// <param name="exponent"></param> /// <returns></returns> public ReallyBigInt Power(long exponent) { ReallyBigInt one = new ReallyBigInt("1"); ReallyBigInt result = one; for (long index = 1; index <= exponent; index++) { result = result.Multiplication(this); } return(result); }
/// <summary> /// Raise a ReallyBitInt to a power /// </summary> /// <param name="exponent"></param> /// <returns></returns> public ReallyBigInt Power(ReallyBigInt exponent) { ReallyBigInt one = new ReallyBigInt("1"); ReallyBigInt result = one; for (ReallyBigInt index = one; index.LessThanOrEqual(exponent); index = index.Addition(one)) { result = result.Multiplication(this); } return(result); }
/// <summary> /// Multiply two REallyBigInts /// </summary> /// <param name="number">A number</param> /// <returns>The product</returns> public ReallyBigInt Multiplication(ReallyBigInt number) { ReallyBigInt product = new ReallyBigInt("0"); for (int index = 0; index < number.value.Length; index++) { int digit = int.Parse(number.value.Substring(number.value.Length - index - 1, 1)); ReallyBigInt workNumber = new ReallyBigInt("0"); for (int loop = 0; loop < digit; loop++) { workNumber = workNumber.Addition(this); } workNumber.value = workNumber.value + Zeros(index); product = product.Addition(workNumber); } return(product); }
/// <summary> /// Calculate x to the power y /// </summary> /// <param name="number">The number</param> /// <param name="exponent">The exponent</param> /// <returns>The answer</returns> public static ReallyBigInt Power(ReallyBigInt number, ReallyBigInt exponent) { if (exponent.value.Equals("0")) { return(new ReallyBigInt("1")); } if (exponent.value.Equals("1")) { return(number); } ReallyBigInt result = number; for (ReallyBigInt index = new ReallyBigInt("2"); index.LessThanOrEqual(exponent); index = index.Addition(new ReallyBigInt("1"))) { result = result.Multiplication(number); } return(result); }