Exemple #1
0
        /**
         * Method Name: Factorial <br>
         * Method Purpose: Uses Mult to find factorial of number <br>
         *
         * <hr>
         * Date created: 10/15/2015 <br>
         *
         * <hr>
         * Notes on specifications, special algorithms, and assumptions:
         *   feeds Mult a series of numbers to multiply UInt by in order to find factorial
         *
         * <hr>
         *   @param uint number to find factorial of
         *   @return None
         */
        public static void Factorial(uint n)
        {
            //for(int i = 2; i < n; i++)
            //{
            //    VL.Mult(i);
            //}
            UIntArr = new UInt[n / 2];
            uint[] uintArr = new uint[n / 2];
            uint   ii      = ((n / 2) + 1);

            for (uint i = 0; i < n / 2; i++)
            {
                uintArr[i] = ii;
                ii++;
            }//for
            ii = ((n / 2) - 1);
            for (uint i = 0; i < n / 2; i++)
            {
                UIntArr[i] = new UInt(i + 1);
                UIntArr[i].Mult(uintArr[ii]);
                ii--;
            }//for
            int end = (int)(n / 2 - 1);

            if (n % 2 == 0)
            {
                VL       = new UInt(UIntArr[end]);
                firstAdd = false;
                DnC((end - 1));
            }
            else
            {
                VL.Add(UIntArr[end - 1]);
                firstAdd = false;
                DnC((end - 2));
            }
        }//factorial(uint)
Exemple #2
0
        }//Mult(long)

        /**
         * Method Name: Mult <br>
         * Method Purpose: multiplies very large UInt's <br>
         *
         * <hr>
         * Date created: 10/15/2015 <br>
         *
         * <hr>
         * Notes on specifications, special algorithms, and assumptions:
         *   multiplies numbers together and accounts for overflow
         *
         * <hr>
         *   @param long that UInt is multiplied by
         *   @return None
         */
        public UInt Seperate(long n, long zeros)
        {
            long product;
            UInt output = new UInt();

            for (int i = 0; i < count; i++)
            {
                UInt tempUInt  = new UInt();
                long tempZeros = zeros;
                int  index     = i;
                while (tempZeros > 8)
                {
                    output.setX(i, 0);
                    tempZeros -= 9;
                    index     += 1;
                }
                if (x[i] == 0)
                {
                    continue;
                }
                if (tempZeros > 0)
                {
                    product = (long)checked (n * x[i] * Math.Pow(10, tempZeros));
                }
                else
                {
                    product = checked (n * x[i]);
                }
                if (product < max)
                {
                    tempUInt.setX(index, (uint)product);
                    tempUInt.setCount(index + 1);
                }//if
                else
                {
                    tempUInt.setX(index, UInt32.Parse(product.ToString().Substring(product.ToString().Length - 9, 9))); //finds last 9 digits and sets to x[i]
                    product = UInt32.Parse(product.ToString().Substring(0, product.ToString().Length - 9));             //finds remaining digits and sets it to product
                    if (product > max)
                    {
                        tempUInt.setX(index + 1, UInt32.Parse(product.ToString().Substring(product.ToString().Length - 9, 9)));
                        product = UInt32.Parse(product.ToString().Substring(0, product.ToString().Length - 9));
                        tempUInt.setX(index + 2, (uint)product);
                        if (tempUInt.getCount() < index + 3)                                                                      //checks to see count is correct and sets it accordingly
                        {
                            tempUInt.setCount(index + 3);
                        }
                    }//if
                    else
                    {
                        tempUInt.setX(index + 1, (uint)product);                                                           //stores carry for adding to next slot
                        if (tempUInt.getCount() < index + 2)                                                               //checks to see count is correct and sets it accordingly
                        {
                            tempUInt.setCount(index + 2);
                        }
                    } //else
                }     //else

                output.Add(tempUInt);
            }
            return(output);
        }//Mult(long)