using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Collections; namespace MultiplyBigNumbers { class MainProgram { public const int length = 100; public const int lengthIndex = 99; public const int n = 199; public const int nIndex = 198; public const string readFile = "numbers.txt"; public const string writeFile = "answers.txt"; static void Main(string[] args) { Stopwatch st = new Stopwatch(); st.Start(); RunProcess(); st.Stop(); Console.WriteLine("Elapsed = {0}.", st.Elapsed.ToString()); if (Stopwatch.IsHighResolution) Console.WriteLine("Timed with high resolution."); else Console.WriteLine("Not timed with high resolution."); Console.ReadLine(); } #region RunProcess private static void RunProcess() { StreamReader sr = new StreamReader(readFile); StreamWriter sw = new StreamWriter(writeFile, false); string numberString1; string numberString2; while (!sr.EndOfStream) { numberString1 = sr.ReadLine(); numberString2 = sr.ReadLine(); sw.WriteLine(MultiplyStrings(numberString1, numberString2)); } } #endregion public static string MultiplyStrings(string numberString1, string numberString2) { StringBuilder result = new StringBuilder(); int[] string1Transform = new int[length]; int[] string2Transform = new int[length]; int j; int k; int answer; int carryover = 0; int i = lengthIndex; while (i > -1) { string1Transform[i] = Convert.ToInt32(numberString1[i].ToString()); string2Transform[i] = Convert.ToInt32(numberString2[i].ToString()); j = i; k = lengthIndex; while (j < length) { carryover += string1Transform[j] * string2Transform[k]; j++; k--; } answer = carryover; carryover = answer / 10; answer -= carryover * 10; result.Insert(0, answer.ToString(), 1); i--; } i = length; while (i < n) { j = 0; k = nIndex - i; while (k > -1) { carryover += string1Transform[j] * string2Transform[k]; j++; k--; } answer = carryover; carryover = answer / 10; answer -= carryover * 10; result.Insert(0, answer.ToString(), 1); i++; } result.Insert(0, carryover.ToString(), 1); return result.ToString().TrimStart(new char[] { '0' }); } } }