Advertisement
zlatkov

Factorial

Jan 13th, 2013
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Factorial
  8. {
  9.     class BigInteger
  10.     {
  11.         static readonly int Base = 10000000; //Because 100 * Base < Int.MaxValue
  12.         List<int> digits = new List<int>();
  13.  
  14.         public BigInteger()
  15.         {
  16.         }
  17.  
  18.         public BigInteger(int x)
  19.         {
  20.             digits.Add(x);
  21.         }
  22.  
  23.         public int NumberOfDigits
  24.         {
  25.             get
  26.             {
  27.                 return digits.Count;
  28.             }
  29.         }
  30.  
  31.         public int this[int index]
  32.         {
  33.             get
  34.             {
  35.                 if (index >= 0 && index < NumberOfDigits)
  36.                 {
  37.                     return digits[index];
  38.                 }
  39.                 else
  40.                 {
  41.                     return 0;
  42.                 }
  43.             }
  44.             set
  45.             {
  46.                 if (index >= 0 && index < NumberOfDigits)
  47.                 {
  48.                     digits[index] = value;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public void AddDigit(int x)
  54.         {
  55.             digits.Add(x);
  56.         }
  57.  
  58.         public static BigInteger operator *(int x, BigInteger y)
  59.         {
  60.             BigInteger result = new BigInteger();
  61.             int carry = 0;
  62.  
  63.             for (int i = 0; i < y.NumberOfDigits; ++i)
  64.             {
  65.                 carry += x * y[i];
  66.  
  67.                 result.AddDigit(carry % Base);
  68.                 carry /= Base;
  69.             }
  70.  
  71.             while (carry > 0)
  72.             {
  73.                 result.AddDigit(carry % Base);
  74.                 carry /= Base;
  75.             }
  76.  
  77.             return result;
  78.         }
  79.  
  80.         public override string ToString()
  81.         {
  82.             StringBuilder result = new StringBuilder("");
  83.             if (NumberOfDigits > 0)
  84.             {
  85.                 result.Append(digits[NumberOfDigits - 1].ToString());
  86.                 for (int i = NumberOfDigits - 2; i >= 0; --i)
  87.                 {
  88.                     result.Append(String.Format("{0:D7}", digits[i]));
  89.                 }
  90.             }
  91.             return result.ToString();
  92.         }
  93.     }
  94.     class Factorial
  95.     {
  96.         static BigInteger CalculateFactorial(int n)
  97.         {
  98.             BigInteger result = new BigInteger(1);
  99.             for (int i = 2; i <= n; ++i)
  100.             {
  101.                 result = i * result;
  102.             }
  103.  
  104.             return result;
  105.         }
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             int n = int.Parse(Console.ReadLine());
  110.  
  111.             BigInteger result = CalculateFactorial(n);
  112.             Console.WriteLine(result.ToString());
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement