public void Smooth_Numbers2(BigInteger N1)
        {
            BigInteger sqrt_N1 = SquareRoot(N1);
            BigInteger i       = sqrt_N1 + 1;
            BigInteger j       = sqrt_N1 - 1;

            // prime number factors
            Factor_Base(N1);
            uint N_smooths = (uint)(factor_base.Length * 1.1);

            if ((N_smooths & 1) == 1)
            {
                N_smooths++;                // make it even
            }
            Qx = new smooth_num[N_smooths];
            Qx.Initialize();

            smooth_num[] Q1x = new smooth_num[N_smooths];
            Q1x.Initialize();

            long k = 0;

            sw1.Restart();

            // Collect smooth numbers
            while (k < N_smooths)
            {
                uint n = 0;
                while (n < Q1x.Length)
                {
                    Q1x[n].Q_of_x = N1 - j * j;
                    Q1x[n].x      = j;
                    j--;
                    n++;

                    Q1x[n].Q_of_x = i * i - N1;
                    Q1x[n].x      = i;
                    i++;
                    n++;
                }

                CancellationTokenSource cancellationSource = new CancellationTokenSource();
                ParallelOptions         options            = new ParallelOptions();
                options.CancellationToken = cancellationSource.Token;
                Parallel.For(0, Q1x.Length, options, (ii, loopState) =>
                {
                    uint[] expo1 = GetPrimeFactors(Q1x[ii].Q_of_x);
                    try
                    {
                        if (expo1 != null)
                        {
                            Qx[k].Q_of_x    = Q1x[ii].Q_of_x;           // save the smooth number
                            Qx[k].x         = Q1x[ii].x;                // save the square root
                            Qx[k].exponents = expo1;                    // save the prime exponents
                            Interlocked.Increment(ref k);
                        }
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        loopState.Stop();
                    }
                }
                             );
                Console.Write(k.ToString() + " smooth numbers\r");
            }   // while (k < factor_base.Length)

            sw1.Stop();
            string strElapsed;

            if (sw1.ElapsedMilliseconds <= 1000)
            {
                strElapsed = String.Format("{0} ms", sw1.ElapsedMilliseconds);
            }
            else
            {
                strElapsed = String.Format("{0:F1} s", (float)sw1.ElapsedMilliseconds / 1000);
            }

            Console.WriteLine("Collected {0} smooth numbers.\nElapsed time: {1}\n", k, strElapsed);
        }
示例#2
0
        public void Smooth_Numbers2(BigInteger N1)
        {
            BigInteger sqrt_N1 = SquareRoot(N1);
            BigInteger i = sqrt_N1 + 1;
            BigInteger j = sqrt_N1 - 1;

            // prime number factors
            Factor_Base(N1);
            uint N_smooths = (uint)(factor_base.Length * 1.1);
            Qx = new smooth_num[N_smooths];
            Qx.Initialize();

            smooth_num[] Q1x = new smooth_num[ARRAY_SIZE];
            Q1x.Initialize();

            long k = 0;
            sw1.Restart();

            // Collect smooth numbers
            while (k < N_smooths)
            {
                for (uint n = 0; n < Q1x.Length; n += 2)
                {
                    Q1x[n].Q_of_x = N1 - j * j;
                    Q1x[n].x = j;
                    j--;

                    Q1x[n + 1].Q_of_x = i * i - N1;
                    Q1x[n + 1].x = i;
                    i++;
                }

                CancellationTokenSource cancellationSource = new CancellationTokenSource();
                ParallelOptions options = new ParallelOptions();
                options.CancellationToken = cancellationSource.Token;
                Parallel.For(0, Q1x.Length, options, (ii, loopState) =>
                        {
                            uint[] expo1 = GetPrimeFactors(Q1x[ii].Q_of_x);
                            try
                            {
                                if (expo1 != null)
                                {
                                    Qx[k].Q_of_x = Q1x[ii].Q_of_x;      // save the smooth number
                                    Qx[k].x = Q1x[ii].x;                // save the square root
                                    Qx[k].exponents = expo1;            // save the prime exponents
                                    Interlocked.Increment(ref k);
                                }
                            }
                            catch (IndexOutOfRangeException e)
                            {
                                loopState.Stop();
                            }
                        }
                );
                Console.Write(k.ToString() + " smooth numbers\r");
            }   // while (k < factor_base.Length)

            sw1.Stop();
            string strElapsed;
            if (sw1.ElapsedMilliseconds <= 1000)
                strElapsed = String.Format("{0} ms", sw1.ElapsedMilliseconds);
            else
                strElapsed = String.Format("{0:F1} s", (float)sw1.ElapsedMilliseconds / 1000);

            Console.WriteLine("Collected {0} smooth numbers.\nElapsed time: {1}\n", k, strElapsed);
        }