public static Bignumber GetFactorial(int n) { long[] aux2 = { 1 }; Bignumber oneresult = new Bignumber(aux2, aux2.Length); long[] aux1 = { n }; Bignumber nresult = new Bignumber(aux1, aux1.Length); if (n == 1) { return(oneresult); } else { return(nresult * GetFactorial(n - 1)); } /* * long[] numberaux = new long[1]; * numberaux[0] = n; * Bignumber number = new Bignumber(numberaux, numberaux.Length); * for (long i = n-1; i >= 1; i--) * { * long[] aux = new long[1]; * aux[0] = i; * number = number *( new Bignumber(i-1)); * } * return number;*/ }
public static Bignumber operator *(Bignumber a, Bignumber b) { int len1 = a.length; int len2 = b.length; long[] result = new long[len1 + len2]; int i_n1 = 0; int i_n2 = 0; int i; for (i = len1 - 1; i >= 0; i--) { long carry = 0; long n1 = a.GetElement(i); i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { long n2 = b.GetElement(j); long sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; // Store result result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) { result[i_n1 + i_n2] += carry; } i_n1++; } long[] auxresult = new long[result.Length]; int k = 0; for (int m = result.Length - 1; m >= 0; m--) { auxresult[k] = result[m]; k++; } Bignumber finalresult = new Bignumber(auxresult, auxresult.Length); return(finalresult); }
static void Main(string[] args) { long[] a = { 1, 2, 3, 4, 5 }; long[] b = { 7, 7, 7 }; int l1 = a.Length; int l2 = b.Length; Bignumber a1 = new Bignumber(a, a.Length); Bignumber b1 = new Bignumber(b, b.Length); Console.WriteLine(b1 + a1); Console.WriteLine(b1 * a1); Console.WriteLine(Fibonacci.GetFibonacci(10)); Console.WriteLine(Factorial.GetFactorial(11)); Console.ReadKey(); }
public static Bignumber GetFibonacci(int n) { long[] aux1 = { 0 }; Bignumber zeroresult = new Bignumber(aux1, aux1.Length); long[] aux2 = { 1 }; Bignumber oneresult = new Bignumber(aux2, aux2.Length); if (n == 0) { return(zeroresult); } if (n == 1) { return(oneresult); } else { return(GetFibonacci(n - 1) + GetFibonacci(n - 2)); } }
public static Bignumber AuxSum(Bignumber a, Bignumber b) { int l1 = a.length; int l2 = b.length; long[] sum = new long[l1 + 1]; int i = l1 - 1; int j = l2 - 1; int k = l1; long carry = 0; long s = 0; while (j >= 0) { s = a.GetElement(i) + b.GetElement(j) + carry; sum[k] = s % 10; carry = s / 10; i--; j--; k--; } while (i >= 0) { s = a.GetElement(i) + carry; sum[k] = s % 10; carry = s / 10; i--; k--; } if (carry == 1) { sum[0] = 1; } Bignumber finalsum = new Bignumber(sum, sum.Length); return(finalsum); }