Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication2
- {
- class BigInteger
- {
- private int[] number;
- private int length;
- public BigInteger(long number)
- {
- string fake = number.ToString();
- this.number = new int[(int)Math.Ceiling(fake.Length/4.0)];
- length = this.number.Length;
- for (int i = fake.Length; i < this.number.Length * 4; i++)
- {
- fake = "0" + fake;
- }
- for (int i = 0; i < this.number.Length; i++)
- {
- this.number[i] = Convert.ToInt32(fake.Substring(i * 4, 4));
- }
- Array.Reverse(this.number);
- }
- private BigInteger(int[] number)
- {
- this.number = number;
- length = number.Length;
- }
- public int getLength()
- {
- return length;
- }
- public static BigInteger operator+(BigInteger first, BigInteger second)
- {
- int[] resultArr = new int[Math.Max(first.length, second.length) + 1];
- int remain = 0;
- for (int i = 0; i < resultArr.Length ; i++)
- {
- if (i < first.length && i < second.length)
- {
- resultArr[i] = remain + first.number[i] + second.number[i];
- remain = resultArr[i] / 10000;
- resultArr[i] = resultArr[i] % 10000;
- }
- else if (i < first.length)
- {
- resultArr[i] = remain + first.number[i];
- remain = resultArr[i] / 10000;
- resultArr[i] = resultArr[i] % 10000;
- }
- else if (i < second.length)
- {
- resultArr[i] = remain + first.number[i];
- remain = resultArr[i] / 10000;
- resultArr[i] = resultArr[i] % 10000;
- }
- else
- {
- resultArr[i] = remain;
- remain = 0;
- }
- }
- return new BigInteger(resultArr);
- }
- public static BigInteger operator*(BigInteger first, BigInteger second)
- {
- BigInteger result = new BigInteger(new int[first.length * second.length + 1]);
- int[] biggest = (first.length >= second.length ? first.number : second.number);
- int[] smallest = (first.length < second.length ? first.number : second.number);
- int remain = 0;
- int[] temp;
- for (int i = 0; i < smallest.Length; i++)
- {
- temp = new int[biggest.Length + i + 1];
- for (int j = 0; j < biggest.Length; j++)
- {
- temp[i+j] += remain + smallest[i] * biggest[j];
- remain = temp[i+j] / 10000;
- temp[i+j] %= 10000;
- }
- temp[i + biggest.Length] = remain;
- remain = 0;
- result += new BigInteger(temp);
- }
- return result;
- }
- public override string ToString()
- {
- string result = "";
- for (int i = length - 1; i >= 0; i--)
- {
- string addition = number[i].ToString();
- for (int j = addition.Length; j < 4; j++ )
- {
- addition = "0" + addition;
- }
- result += addition;
- }
- while (result.StartsWith("0"))
- {
- result = result.Substring(1);
- }
- return result;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- BigInteger big = new BigInteger(10000);
- BigInteger notSoBig = new BigInteger(10000);
- BigInteger result = big * notSoBig;
- Console.WriteLine(big * notSoBig);
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement