Ejemplo n.º 1
0
        //Get multiplication of two big numbers
        public AccurateFactorial multiply(params AccurateFactorial[] af)
        {
            AccurateFactorial mult = new AccurateFactorial();

            for (int k = 0; k < 2; k++)
            {
                DisposeZero(af[0]);                        //prepare the numbers to work
            }
            //Set the size of new array
            int length = af[0].value.Length + af[1].value.Length,
                len1 = af[0].value.Length - 1, len2 = af[1].value.Length - 1;

            mult.SetSize(length);
            //implement the multiplication algorithm
            length--; int reminder  = 0;
            int           MaxLength = length;

            for (int i = len1; i >= 0; i--)
            {
                for (int k = len2; k >= 0; k--)
                {
                    int temp = reminder;
                    temp                  = af[0].value[i] * af[1].value[k];
                    reminder             += temp / 10;
                    mult.value[length]   += temp % 10;
                    mult.value[--length] += reminder;
                    reminder              = 0;
                }
                MaxLength--;
                length = MaxLength;
            }
            reminder = 0;
            length   = mult.value.Length - 1;
            //Final summarization
            for (int i = length; i > 0; i--)
            {
                mult.value[i - 1] += mult.value[i] / 10;
                mult.value[i]      = mult.value[i] % 10;
            }
            mult = DisposeZero(mult);
            //Result
            return(mult);
        }
Ejemplo n.º 2
0
        //Dispose from leading zeros
        public AccurateFactorial DisposeZero(AccurateFactorial n)
        {
            if (n.value.Length > 1)
            {
                int len = 0, i = 0;
                while (i < n.value.Length - 1 && n.value[i] == 0)
                {
                    len++;
                    i++;
                }
                AccurateFactorial temp = new AccurateFactorial();

                temp.SetSize(n.value.Length - len);
                for (i = 0; i < n.value.Length - len; i++)
                {
                    temp.value[i] = n.value[len + i];
                }
                return(temp);
            }
            return(n);
        }