Advertisement
AlexPopov

Methods

Jan 26th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 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. using System.Numerics;
  7.  
  8. namespace Multiplication
  9. {
  10.     class Multiplication
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             BigInteger num1 = BigInteger.Parse(Console.ReadLine());
  15.             BigInteger num2 = BigInteger.Parse(Console.ReadLine());
  16.             List<int> finalResult = MultiplyBigNumbers(num1, num2);
  17.             PrintList(finalResult);
  18.         }
  19.  
  20.         static List<int> MultiplyBigNumbers(BigInteger num1, BigInteger num2)
  21.         {
  22.             //make the smaller number to be first
  23.             Swap(ref num1, ref num2);
  24.             List<int> list1 = ConvertBigIntToList(num1);
  25.             List<int> list2 = ConvertBigIntToList(num2);
  26.             //create a list of lists with the multiplication of the digits of the shorter number with the longer number
  27.             List<List<int>> helpList = new List<List<int>>();
  28.             for (int i = 0; i < list1.Count; i++)
  29.             {
  30.                 helpList.Add(ListAndNumber(list2, list1[i]));
  31.             }
  32.  
  33.             AppendZeros(helpList);
  34.  
  35.             List<int> finalResult = new List<int>();
  36.             int digit = 0;
  37.             int carry = 0;
  38.             for (int i = 0; i < helpList[helpList.Count - 1].Count; i++)
  39.             {
  40.                 int product = carry;
  41.                 for (int j = 0; j < helpList.Count; j++)
  42.                 {
  43.                     product += helpList[j][i];
  44.                 }
  45.                 if (product >= 10)
  46.                 {
  47.                     digit = product % 10;
  48.                     carry = product / 10;
  49.                 }
  50.                 else
  51.                 {
  52.                     digit = product;
  53.                     carry = 0;
  54.                 }
  55.                 finalResult.Add(digit);
  56.             }
  57.             finalResult.Add(carry);
  58.  
  59.             return finalResult;
  60.  
  61.         }
  62.  
  63.         static void AppendZeros(List<List<int>> helpList)
  64.         {
  65.             int zeros = helpList.Count - 1;
  66.             for (int i = 0; i < helpList.Count; i++)
  67.             {
  68.                 for (int j = 0; j < zeros; j++)
  69.                 {
  70.                     helpList[i].Add(0);
  71.                 }
  72.                 for (int j = 0; j < i; j++)
  73.                 {
  74.                     helpList[i].Insert(0, 0);
  75.                 }
  76.  
  77.                 if (zeros > 0)
  78.                 {
  79.                     zeros--;
  80.                 }
  81.                 else
  82.                 {
  83.                     break;
  84.                 }
  85.             }
  86.         }
  87.  
  88.         //multiplies an array of ints and a single digit
  89.         static List<int> ListAndNumber(List<int> list, int num)
  90.         {
  91.             List<int> result = new List<int>();
  92.             int product = 0;
  93.             int carry = 0;
  94.             int digit = 0;
  95.             for (int i = 0; i < list.Count; i++)
  96.             {
  97.                 product = list[i] * num + carry;
  98.                 if (product >= 10)
  99.                 {
  100.                     digit = product % 10;
  101.                     carry = product / 10;
  102.                 }
  103.                 else
  104.                 {
  105.                     digit = product;
  106.                     carry = 0;
  107.                 }
  108.                 result.Add(digit);
  109.             }
  110.             result.Add(carry);
  111.             return result;
  112.         }
  113.  
  114.         //extract the digits of a number into an array;
  115.         static List<int> ConvertBigIntToList(BigInteger n)
  116.         {
  117.             List<int> digits = new List<int>();
  118.             while (n > 0)
  119.             {
  120.                 digits.Add((int)(n % 10));
  121.                 n /= 10;
  122.             }
  123.             return digits;
  124.         }
  125.  
  126.         //Print a list of digits in the following order: first prints the last element if other than 0. The last element it prints is also the last digit of the number.
  127.         static void PrintList(List<int> list)
  128.         {
  129.             if (list[list.Count - 1] != 0)
  130.             {
  131.                 Console.Write(list[list.Count - 1]);
  132.             }
  133.             for (int i = list.Count - 2; i >= 0; i--)
  134.             {
  135.                 Console.Write(list[i]);
  136.             }
  137.            
  138.             Console.WriteLine();
  139.         }
  140.  
  141.         //A method to set the smaller number to be first
  142.         static void Swap(ref BigInteger a, ref BigInteger b)
  143.         {
  144.             if (a > b)
  145.             {
  146.                 a = a + b;
  147.                 b = a - b;
  148.                 a = a - b;
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement