Advertisement
stak441

Methods - 10 - CalcFactorials

Jan 21st, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _10.CalculateFactorial
  7. {
  8.     class Program
  9.     {
  10.         static int[] SumOfArrays(int[] firstArray, int[] secondArray)
  11.         {
  12.             bool firstArrayShorter = (firstArray.Length < secondArray.Length);
  13.             int minLength = 0;
  14.             int maxLength = 0;
  15.             if (firstArrayShorter)
  16.             {
  17.                 minLength = firstArray.Length;
  18.                 maxLength = secondArray.Length;
  19.             }
  20.             else
  21.             {
  22.                 minLength = secondArray.Length;
  23.                 maxLength = firstArray.Length;
  24.             }
  25.  
  26.             int[] sumArray = new int[maxLength + 1];         //size is 1 more, because if the 2 sizes are equal, the last digits may sum to more than 10                              
  27.             bool largerThanTen = false;
  28.  
  29.             for (int i = 0; i < sumArray.Length; i++)
  30.             {
  31.                 int tempSum = 0;
  32.                 if (i < minLength)                      // First part - sums both arrays
  33.                 {
  34.                     tempSum = firstArray[i] + secondArray[i];
  35.                 }
  36.                 else if (i >= minLength && i < sumArray.Length - 1)         //Second part - sums only the longer array
  37.                 {
  38.                     if (firstArrayShorter)
  39.                     {
  40.                         tempSum += secondArray[i];
  41.                     }
  42.                     else
  43.                     {
  44.                         tempSum += firstArray[i];
  45.                     }
  46.                 }
  47.  
  48.                 if (largerThanTen)                           //Adds 1 if the previous sum was > 10
  49.                 {
  50.                     tempSum += 1;
  51.                 }
  52.  
  53.                 if (tempSum > 9)
  54.                 {
  55.                     largerThanTen = true;
  56.                     sumArray[i] = tempSum % 10;
  57.                 }
  58.                 else
  59.                 {
  60.                     largerThanTen = false;
  61.                     sumArray[i] = tempSum;
  62.                 }
  63.             }
  64.  
  65.             if (sumArray[sumArray.Length - 1] == 0)
  66.             {
  67.                 Array.Resize(ref sumArray, sumArray.Length - 1);
  68.             }
  69.             return sumArray;
  70.  
  71.         }
  72.  
  73.         static int[] MultiplyArrays(int[] factorialArray, int position)
  74.         {
  75.             int[] dummyArray = { 0 };
  76.  
  77.             for (int i = 0; i < position; i++)
  78.             {
  79.                 dummyArray = SumOfArrays(factorialArray, dummyArray);
  80.             }
  81.  
  82.             return dummyArray;
  83.         }
  84.  
  85.         static void Main(string[] args)
  86.         {
  87.             int[] factorialArray = { 1 };
  88.            
  89.             int n = 100;                     //Number to calculate factorial
  90.  
  91.             for (int i = 1; i <= n; i++)
  92.             {
  93.                 int[] result = MultiplyArrays(factorialArray, i);
  94.                 factorialArray = result;
  95.             }
  96.  
  97.             for (int i = factorialArray.Length-1; i >= 0; i--)        //Because the array is reversed, it starts from the end
  98.             {
  99.                 Console.Write(factorialArray[i]);
  100.             }
  101.             Console.WriteLine();
  102.         }
  103.  
  104.        
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement