Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program to calculate n! for each n in the range[1..100]. Hint: Implement first a method
- //that multiplies a number represented as array of digits by given integer number.
- namespace Methods10
- {
- using System;
- using System.Text;
- using System.Numerics;
- class FactorielMethod
- {
- public static int[] NumberMultiplyer(int[] arr, int digit)
- {
- int len = arr.Length;
- int[] tempArr = new int[len];
- int[] tempArr2 = new int[len];
- // Console.WriteLine("resultArr " + string.Join("", arr));
- for (int i = 0; i < arr.Length - 1; i++) // {0, 2, 1} 0 2 1
- {
- if (arr[i] != 0)
- {
- for (int k = 0; k < len; k++)
- {
- tempArr[k] = 0;
- }
- int current = digit * arr[i];
- for (int j = i; j < i + 3 ; j++)
- {
- tempArr[j] = current % 10;
- current = current / 10;
- }
- tempArr2 = addArraysOfDigits(tempArr, tempArr2);
- }
- }
- return tempArr2;
- }
- static int [] addArraysOfDigits(int[] arr1, int[] arr2)
- {
- int maxLen = arr1.Length > arr2.Length ? arr1.Length : arr2.Length;
- int[] tempArr = new int[maxLen + 1];
- int[] sum = new int[maxLen + 1];
- if (arr1.Length > arr2.Length)
- {
- for (int i = 0; i < maxLen; i++)
- {
- if (i < arr2.Length)
- {
- tempArr[i] = arr2[i];
- }
- else
- {
- tempArr[i] = 0;
- }
- }
- for (int i = 0; i < sum.Length; i++)
- {
- if (i < arr1.Length)
- {
- sum[i] = (arr1[i] + tempArr[i]) % 10;
- }
- if (i > 0 && arr1[i - 1] + tempArr[i - 1] >= 10)
- {
- sum[i] += 1;
- }
- }
- }
- else
- {
- for (int i = 0; i < maxLen; i++)
- {
- if (i < arr1.Length)
- {
- tempArr[i] = arr1[i];
- }
- else
- {
- tempArr[i] = 0;
- }
- }
- for (int i = 0; i < sum.Length; i++)
- {
- if (i < arr2.Length)
- {
- sum[i] = (arr2[i] + tempArr[i]) % 10;
- }
- if (i > 0 && arr2[i - 1] + tempArr[i - 1] >= 10)
- {
- sum[i] += 1;
- }
- }
- }
- return sum;
- }
- public static void Main()
- {
- while (true)
- {
- Console.Write("Insert value[0, 100] : ");
- int n = int.Parse(Console.ReadLine());
- int[] answer = new int[n * n + 1]; // +1 for n = 0
- answer[0] = 1;
- for (int i = 2; i <= n; i++) // 0 and 1 ! are 1
- {
- answer = NumberMultiplyer(answer, i);
- }
- string final = "";
- for (int i = 0; i < answer.Length; i++)
- {
- final = answer[i] + final;
- }
- BigInteger finalResult = BigInteger.Parse(final);
- Console.WriteLine("My result: \t" + finalResult);
- // check
- BigInteger factoriel = 1;
- int m = 1;
- while (m <= n)
- {
- factoriel = factoriel * m;
- m++;
- }
- Console.WriteLine("{0}! = \t\t{1}", n, factoriel);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement