public string Compute(IList <string> numbers) { StringMatrixTransformer smt = new StringMatrixTransformer(); ArithmeticUtils au = new ArithmeticUtils(); IList <IList <int> > matrix = smt.TransformStringListToReversedIntMatrix(numbers); int lMax = 0, carry = 0; StringBuilder sb = new StringBuilder(); foreach (List <int> l in matrix) { lMax = lMax < l.Count ? l.Count : lMax; } for (int i = 0; i < lMax; i++) { int x = carry; carry = 0; foreach (List <int> l in matrix) { x += l.ElementAtOrDefault(i); } int y = 0; au.GetCarryBase10(ref x, ref y, ref carry); sb.Append(y); } return(smt.ReverseString(sb.ToString())); }
public string Compute(IList <string> numbers) { if (numbers.Count != 2) { throw new Exception("Invalid number of arguments, 2 expected, found: " + numbers.Count); } StringMatrixTransformer smt = new StringMatrixTransformer(); ArithmeticUtils au = new ArithmeticUtils(); IList <IList <int> > reversed = smt.TransformStringListToReversedIntMatrix(numbers); StringBuilder sb = new StringBuilder(); int carry = 0, y = 0; int n = reversed.ElementAtOrDefault(1).ElementAtOrDefault(0); IList <int> number = reversed.ElementAtOrDefault(0); foreach (int bn in number) { int x = carry; x += bn * n; au.GetCarryBase10(ref x, ref y, ref carry); sb.Append(y); } if (carry != 0) { sb.Append(carry); } return(smt.ReverseString(sb.ToString())); }
public string Compute(IList <string> numbers) { if (numbers.Count < 2) { throw new Exception("Need at least 2 numbers to multiply, found: " + numbers.Count); } StringMatrixTransformer smt = new StringMatrixTransformer(); ILargeNumberComputer a = new LargeNumberAdder(); ArithmeticUtils au = new ArithmeticUtils(); string num = numbers[0]; IList <IList <int> > matrix = smt.TransformStringListToReversedIntMatrix(numbers); for (int i = 1; i < numbers.Count; i++) { IList <int> m = matrix.ElementAt(i); ILargeNumberComputer lom = new LargeToOneNumberMultiplier(); IList <string> stageIntermediates = new List <string>(); for (int j = 0; j < m.Count; j++) { string prefix = ""; int n = m[j]; //add the 0 to handle decimal place of the multiplying number for (int k = 0; k < j; k++) { prefix += "0"; } stageIntermediates.Add(lom.Compute(new List <string>() { num, n.ToString() }) + prefix); } num = a.Compute(stageIntermediates); } return(num); }
public string Compute(IList <string> numbers) { if (numbers.Count > 2) { throw new Exception("Incorrect count of numbers to subtract, expecting 2, found: " + numbers.Count.ToString()); } StringMatrixTransformer smt = new StringMatrixTransformer(); NumercStringUtils nsu = new NumercStringUtils(); bool negateAnswer = ReorderLargerFirst(ref numbers); IList <IList <int> > matrix = smt.TransformStringListToReversedIntMatrix(numbers); StringBuilder sb = new StringBuilder(); int i = 0; for (; i < matrix[1].Count; i++) { if (matrix[0][i] < matrix[1][i]) { SetupCarryAt(matrix[0], i); } sb.Append(matrix[0][i] - matrix[1][i]); } // get the rest if there's any left in the larger number for (; i < matrix[0].Count; i++) { sb.Append(matrix[0][i]); } return(nsu.TrimLeadingZeros( negateAnswer ? "-" + smt.ReverseString(sb.ToString()) : smt.ReverseString(sb.ToString()))); }