Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 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 ConsoleApplication2
  8. {
  9.     class BigInteger
  10.     {
  11.         private int[] number;
  12.         private int length;
  13.         public BigInteger(long number)
  14.         {
  15.             string fake = number.ToString();
  16.             this.number = new int[(int)Math.Ceiling(fake.Length/4.0)];
  17.             length = this.number.Length;
  18.             for (int i = fake.Length; i < this.number.Length * 4; i++)
  19.             {
  20.                 fake = "0" + fake;
  21.             }
  22.             for (int i = 0; i < this.number.Length; i++)
  23.             {
  24.                 this.number[i] = Convert.ToInt32(fake.Substring(i * 4, 4));
  25.             }
  26.             Array.Reverse(this.number);
  27.         }
  28.         private BigInteger(int[] number)
  29.         {
  30.             this.number = number;
  31.             length = number.Length;
  32.         }
  33.         public int getLength()
  34.         {
  35.             return length;
  36.         }
  37.         public static BigInteger operator+(BigInteger first, BigInteger second)
  38.         {
  39.             int[] resultArr = new int[Math.Max(first.length, second.length) + 1];
  40.             int remain = 0;
  41.             for (int i = 0; i < resultArr.Length ; i++)
  42.             {
  43.                 if (i < first.length && i < second.length)
  44.                 {
  45.                     resultArr[i] = remain + first.number[i] + second.number[i];
  46.                     remain = resultArr[i] / 10000;
  47.                     resultArr[i] = resultArr[i] % 10000;
  48.                 }
  49.                 else if (i < first.length)
  50.                 {
  51.                     resultArr[i] = remain + first.number[i];
  52.                     remain = resultArr[i] / 10000;
  53.                     resultArr[i] = resultArr[i] % 10000;
  54.                 }
  55.                 else if (i < second.length)
  56.                 {
  57.                     resultArr[i] = remain + first.number[i];
  58.                     remain = resultArr[i] / 10000;
  59.                     resultArr[i] = resultArr[i] % 10000;
  60.                 }
  61.                 else
  62.                 {
  63.                     resultArr[i] = remain;
  64.                     remain = 0;
  65.                 }
  66.             }
  67.             return new BigInteger(resultArr);
  68.         }
  69.         public static BigInteger operator*(BigInteger first, BigInteger second)
  70.         {
  71.             BigInteger result = new BigInteger(new int[first.length * second.length + 1]);
  72.             int[] biggest = (first.length >= second.length ? first.number : second.number);
  73.             int[] smallest = (first.length < second.length ? first.number : second.number);
  74.             int remain = 0;
  75.             int[] temp;
  76.             for (int i = 0; i < smallest.Length; i++)
  77.             {
  78.                 temp = new int[biggest.Length + i + 1];
  79.                 for (int j = 0; j < biggest.Length; j++)
  80.                 {
  81.                     temp[i+j] += remain + smallest[i] * biggest[j];
  82.                     remain = temp[i+j] / 10000;
  83.                     temp[i+j] %= 10000;
  84.                 }
  85.                 temp[i + biggest.Length] = remain;
  86.                 remain = 0;
  87.                 result += new BigInteger(temp);
  88.             }
  89.             return result;
  90.         }
  91.         public override string ToString()
  92.         {
  93.             string result = "";
  94.             for (int i = length - 1; i >= 0; i--)
  95.             {
  96.                 string addition = number[i].ToString();
  97.                 for (int j = addition.Length; j < 4; j++ )
  98.                 {
  99.                     addition = "0" + addition;
  100.                 }
  101.                 result += addition;
  102.             }
  103.             while (result.StartsWith("0"))
  104.             {
  105.                 result = result.Substring(1);
  106.             }
  107.             return result;
  108.         }
  109.     }
  110.     class Program
  111.     {
  112.         static void Main(string[] args)
  113.         {
  114.             BigInteger big = new BigInteger(10000);
  115.             BigInteger notSoBig = new BigInteger(10000);
  116.             BigInteger result = big * notSoBig;
  117.             Console.WriteLine(big * notSoBig);
  118.             Console.Read();
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement