Esempio n. 1
0
        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()));
        }
Esempio n. 2
0
            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()));
            }