Пример #1
0
        /**
         * The Factor Base size B is given by (e ^ (sqrt(r))) ^ (sqrt(2)/4)
         *
         * Where r = ln(n) * ln(ln(n))
         *
         * Even for numbers in the millions of trillions, r ends up being very small, so in order to do exact math we assume r can fit inside a double.
         *
         * Since we must have an integer number of factor bases, we take the floor of B.
         **/
        public static int getFactorBaseSize(BigInteger number_to_factor)
        {
            double r = BigInteger.Log(number_to_factor) * (BigInteger.Log(BigIntegerWrapper.Log(number_to_factor)));

            double exact_value = Math.Pow(Math.Pow(Math.E, Math.Sqrt(r)), (Math.Sqrt(2) / 4));

            return((int)Math.Floor(exact_value));
        }
Пример #2
0
        public static int SieveofSundaramSerial(BigInteger to_factor)
        {
            if (BigIntegerWrapper.SqRt(to_factor) < int.MaxValue - 1)
            {
                int BAsize = (int)BigIntegerWrapper.SqRt(to_factor);
                int k      = BAsize / 2;

                BitArray tracker = new BitArray(BAsize);

                /* SET ALL TO PRIME STATUS */
                tracker.SetAll(true);

                int maxVal      = 0;
                int denominator = 0;

                Stopwatch sundaram_serial = new Stopwatch();
                sundaram_serial.Start();

                for (int i = 1; i < k; i++)
                {
                    denominator = (i << 1) + 1;
                    maxVal      = (k - i) / denominator;
                    for (int j = i; j <= maxVal; j++)
                    {
                        tracker[i + j * denominator] = false;
                    }
                }
                sundaram_serial.Stop();
                Stopwatch serielsearch = new Stopwatch();
                serielsearch.Start();

                int dummy = 0;
                for (int i = 1; i < k; i++)
                {
                    if (tracker[i])
                    {
                        dummy = (i << 1) + 1; // dummy contains prime number.The code is here not ignore the prime number calcuation part.
                        if (to_factor % dummy == 0)
                        {
                            break;
                        }
                    }
                }
                serielsearch.Stop();

                Console.WriteLine("search runtime: " + serielsearch.ElapsedMilliseconds + "ms.");
                Console.WriteLine("Serial Sieve of Sundaram Runtime: " + sundaram_serial.ElapsedMilliseconds + "ms.");

                return(dummy);
            }
            else
            {
                Console.WriteLine("Too big to factor with Sieve of Sundaram. ");
                return(-1);
            }
        }
Пример #3
0
 public static BigInteger computeQX(BigInteger x, BigInteger n)
 {
     return(BigInteger.Pow((x + BigIntegerWrapper.SqRt(n)), 2) % n);
 }
Пример #4
0
        public static int SieveofSundaramParallel(BigInteger to_factor)
        {
            if (BigIntegerWrapper.SqRt(to_factor) < int.MaxValue - 1)
            {
                int BAsize = (int)BigIntegerWrapper.SqRt(to_factor);
                int k      = BAsize / 2;

                BitArray tracker = new BitArray(BAsize);

                /* SET ALL TO PRIME STATUS */
                tracker.SetAll(true);

                int maxVal      = 0;
                int denominator = 0;

                Stopwatch sundaram_parallel = new Stopwatch();
                sundaram_parallel.Start();

                for (int i = 1; i < k; i++)
                {
                    denominator = (i << 1) + 1;
                    maxVal      = (k - i) / denominator;

                    /*Parallel.For(i, maxVal, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (j, state) =>
                     * {
                     *  tracker[i + j * denominator] = false;
                     * });*/
                    for (int j = i; j <= maxVal; j++)
                    {
                        tracker[i + j * denominator] = false;
                    }
                }
                sundaram_parallel.Stop();
                Stopwatch parallelSearch = new Stopwatch();
                parallelSearch.Start();

                int dummy = 0;
                int final = 0;
                Parallel.For(1, k, new ParallelOptions {
                    MaxDegreeOfParallelism = 4
                }, (i, state) =>
                {
                    if (tracker[i])
                    {
                        dummy = (i << 1) + 1; // dummy contains prime number.The code is here not ignore the prime number calcuation part.
                        if (to_factor % dummy == 0)
                        {
                            final = dummy;
                            state.Break();
                        }
                    }
                });
                parallelSearch.Stop();

                Console.WriteLine("search runtime: " + parallelSearch.ElapsedMilliseconds + "ms.");
                Console.WriteLine("Parallel Sieve of Sundaram Runtime: " + sundaram_parallel.ElapsedMilliseconds + "ms.");

                return(final);
            }
            else
            {
                Console.WriteLine("Too big to factor with Sieve of Sundaram. ");
                return(-1);
            }
        }
Пример #5
0
        public BigInteger[] getFactorsSerial()
        {
            BigInteger p = 1997333137;
            BigInteger q = 2106945901;

            BigInteger integer_to_factor = p * q;

            Console.WriteLine("p value: " + p);
            Console.WriteLine("q value: " + q);


            BigInteger[] bruteforce_Serial = LeedamKeyson.factorModulusSerial(integer_to_factor);

            Console.WriteLine("brute force serial p value: " + bruteforce_Serial[0]);
            Console.WriteLine("brute force serial q value: " + bruteforce_Serial[1]);


            BigInteger[] bruteforce_Parallel = LeedamKeyson.factorModulusParallel(integer_to_factor);

            Console.WriteLine("brute force parallel p value: " + bruteforce_Parallel[0]);
            //Console.WriteLine("brute force parallel q value" + bruteforce_Parallel[1]);


            int factor_serial = QuadraticSieveFunctions.SieveofSundaramSerial(integer_to_factor);

            Console.WriteLine("Serial returns the p value: " + factor_serial);
            Console.WriteLine("Serial returns the q value: " + (integer_to_factor / factor_serial));

            int factor_parallel = QuadraticSieveFunctions.SieveofSundaramParallel(integer_to_factor);

            Console.WriteLine("Parallel returns the p value: " + factor_parallel);
            Console.WriteLine("Parallel returns the q value: " + (integer_to_factor / factor_parallel));



            int factor_base_size = QuadraticSieveFunctions.getFactorBaseSize(integer_to_factor);

            factor_base_size = (int)BigIntegerWrapper.SqRt(integer_to_factor);
            BigInteger sieve_interval_upper = QuadraticSieveFunctions.getSieveInterval(factor_base_size);

            int[] factor_base = QuadraticSieveFunctions.SieveOfErastosthenesSerial(factor_base_size);
            factor_base = new int[] { 0 };
            factor_base = QuadraticSieveFunctions.SieveOfErastosthenesParallel(factor_base_size);



            int[] quadratic_residues = QuadraticSieveFunctions.getResiduesSerial(factor_base, integer_to_factor);

            BigInteger sieve_interval       = QuadraticSieveFunctions.getSieveInterval(factor_base_size);
            BigInteger sieve_interval_start = BigIntegerWrapper.SqRt(integer_to_factor);

            LinkedList <BigInteger[]> smooth_numbers = QuadraticSieveFunctions.SmoothNumberSieveSerial(integer_to_factor, quadratic_residues, sieve_interval_start, sieve_interval_start + sieve_interval);
            //int k = 100000000;
            //QuadraticSieveFunctions.getFactorBaseSize(integer_to_factor);
            //int test_factor_base_size = QuadraticSieveFunctions.getFactorBaseSize(k);


            int result  = factorSerial(factor_base, integer_to_factor);
            int result2 = factorParallel(factor_base, integer_to_factor);

            //int[] factor_base_parallel_improved = QuadraticSieveFunctions.SieveOfErastosthenesParallelImproved(2 * k);
            //int[] factor_base_parallel = QuadraticSieveFunctions.SieveOfErastosthenesParallel(2*k);

            return(new BigInteger[] { result, (int)(sieve_interval_upper / result) });
        }
Пример #6
0
 public BigInteger BigRoot(BigInteger k, BigInteger integer_to_factor)
 {
     return(BigInteger.Pow((k + BigIntegerWrapper.SqRt(integer_to_factor)), 2) % integer_to_factor);
 }