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;
- using System.Numerics;
- namespace Multiplication
- {
- class Multiplication
- {
- static void Main(string[] args)
- {
- BigInteger num1 = BigInteger.Parse(Console.ReadLine());
- BigInteger num2 = BigInteger.Parse(Console.ReadLine());
- List<int> finalResult = MultiplyBigNumbers(num1, num2);
- PrintList(finalResult);
- }
- static List<int> MultiplyBigNumbers(BigInteger num1, BigInteger num2)
- {
- //make the smaller number to be first
- Swap(ref num1, ref num2);
- List<int> list1 = ConvertBigIntToList(num1);
- List<int> list2 = ConvertBigIntToList(num2);
- //create a list of lists with the multiplication of the digits of the shorter number with the longer number
- List<List<int>> helpList = new List<List<int>>();
- for (int i = 0; i < list1.Count; i++)
- {
- helpList.Add(ListAndNumber(list2, list1[i]));
- }
- AppendZeros(helpList);
- List<int> finalResult = new List<int>();
- int digit = 0;
- int carry = 0;
- for (int i = 0; i < helpList[helpList.Count - 1].Count; i++)
- {
- int product = carry;
- for (int j = 0; j < helpList.Count; j++)
- {
- product += helpList[j][i];
- }
- if (product >= 10)
- {
- digit = product % 10;
- carry = product / 10;
- }
- else
- {
- digit = product;
- carry = 0;
- }
- finalResult.Add(digit);
- }
- finalResult.Add(carry);
- return finalResult;
- }
- static void AppendZeros(List<List<int>> helpList)
- {
- int zeros = helpList.Count - 1;
- for (int i = 0; i < helpList.Count; i++)
- {
- for (int j = 0; j < zeros; j++)
- {
- helpList[i].Add(0);
- }
- for (int j = 0; j < i; j++)
- {
- helpList[i].Insert(0, 0);
- }
- if (zeros > 0)
- {
- zeros--;
- }
- else
- {
- break;
- }
- }
- }
- //multiplies an array of ints and a single digit
- static List<int> ListAndNumber(List<int> list, int num)
- {
- List<int> result = new List<int>();
- int product = 0;
- int carry = 0;
- int digit = 0;
- for (int i = 0; i < list.Count; i++)
- {
- product = list[i] * num + carry;
- if (product >= 10)
- {
- digit = product % 10;
- carry = product / 10;
- }
- else
- {
- digit = product;
- carry = 0;
- }
- result.Add(digit);
- }
- result.Add(carry);
- return result;
- }
- //extract the digits of a number into an array;
- static List<int> ConvertBigIntToList(BigInteger n)
- {
- List<int> digits = new List<int>();
- while (n > 0)
- {
- digits.Add((int)(n % 10));
- n /= 10;
- }
- return digits;
- }
- //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.
- static void PrintList(List<int> list)
- {
- if (list[list.Count - 1] != 0)
- {
- Console.Write(list[list.Count - 1]);
- }
- for (int i = list.Count - 2; i >= 0; i--)
- {
- Console.Write(list[i]);
- }
- Console.WriteLine();
- }
- //A method to set the smaller number to be first
- static void Swap(ref BigInteger a, ref BigInteger b)
- {
- if (a > b)
- {
- a = a + b;
- b = a - b;
- a = a - b;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement