Advertisement
sashomaga

Implement method like BigInteger Class

Jan 17th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace Factorial
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.  
  12.             List<int> fact = new List<int>();
  13.             fact.Add(1);
  14.             for (int i = 2; i < 100; i++)
  15.             {
  16.                 CalcFactorial(fact, i);
  17.                 Print(fact);
  18.             }
  19.  
  20.  
  21.         }
  22.  
  23.         private static void Print(List<int> fact)
  24.         {
  25.             for (int i = fact.Count - 1; i >= 0; i--)
  26.             {
  27.                 Console.Write(fact[i]);
  28.             }
  29.             Console.WriteLine();
  30.         }
  31.  
  32.         private static void CalcFactorial(List<int> fact, int n)
  33.         {
  34.             List<int> factCopy = new List<int>();
  35.             for (int i = 0; i < fact.Count; i++)
  36.             {
  37.                 factCopy.Add(fact[i]);
  38.             }
  39.  
  40.             int carryTens;
  41.             int carryHundreds;
  42.             int result;
  43.  
  44.             for (int i = factCopy.Count - 1; i >= 0; i--)
  45.             {
  46.                 result = 0;
  47.                 carryTens = 0;
  48.                 carryHundreds = 0;
  49.                 result = factCopy[i] * n;
  50.                 fact[i] = result % 10;
  51.  
  52.  
  53.                 if (result > 99)
  54.                 {
  55.                     carryHundreds = result / 100;
  56.                     carryTens = (result / 10) % 10;
  57.                 }
  58.                 else if (result > 9)
  59.                 {
  60.                     carryTens = result / 10;
  61.                 }
  62.  
  63.                 if (carryTens > 0)
  64.                 {
  65.                     if (fact.Count < i + 2)
  66.                     {
  67.                         fact.Add(carryTens);
  68.                     }
  69.                     else
  70.                     {
  71.                         fact[i + 1] += carryTens;
  72.                         if (fact[i + 1] > 9)
  73.                         {
  74.                             fact[i + 1] %= 10;
  75.                             carryHundreds++;
  76.                         }
  77.  
  78.                     }
  79.                 }
  80.                 if (carryHundreds > 0)
  81.                 {
  82.                     if (fact.Count < i + 3)
  83.                     {
  84.                         fact.Add(carryHundreds);
  85.                     }
  86.                     else
  87.                     {
  88.                         fact[i + 2] += carryHundreds;
  89.                     }
  90.  
  91.  
  92.                 }
  93.  
  94.             }
  95.  
  96.  
  97.         }
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement