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) { 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("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()))); }