コード例 #1
0
ファイル: Factorizer.cs プロジェクト: xgalv/Cryptool2
        public void Execute()
        {
            ProgressChanged(0, 1);

            if (InputNumber <= 0)
            {
                FireOnGuiLogNotificationOccuredEventError("Input must be a natural number > 0");
                return;
            }

            if (m_Settings.Action == 0) // find all prime factors
            {
                Dictionary <BigInteger, long> factors;

                if (m_Settings.BruteForceLimitEnabled)
                {
                    bool isFactorized = false;
                    factors = InputNumber.Factorize(m_Settings.BruteForceLimit, out isFactorized);
                    if (!isFactorized)
                    {
                        FireOnGuiLogNotificationOccuredEvent(string.Format("Brute force limit of {0} reached, the last factor is still composite.", m_Settings.BruteForceLimit), NotificationLevel.Warning);
                    }
                }
                else
                {
                    factors = InputNumber.Factorize();
                }

                List <BigInteger> l = new List <BigInteger>();
                foreach (var f in factors.Keys)
                {
                    for (int i = 0; i < factors[f]; i++)
                    {
                        l.Add(f);
                    }
                }
                l.Sort();
                Factors = l.ToArray();
            }
            else  // find the smallest prime factor
            {
                if (InputNumber == 1)
                {
                    // do nothing
                }
                else if (InputNumber.IsProbablePrime())
                {
                    Factor    = InputNumber;
                    Remainder = 1;
                }
                else
                {
                    BigInteger sqrt  = InputNumber.Sqrt();
                    BigInteger limit = sqrt;
                    if (m_Settings.BruteForceLimitEnabled && limit > m_Settings.BruteForceLimit)
                    {
                        limit = m_Settings.BruteForceLimit;
                    }
                    int progressdisplay = 0;

                    for (BigInteger factor = 2; factor <= sqrt; factor = (factor + 1).NextProbablePrime())
                    {
                        if (m_Settings.BruteForceLimitEnabled && factor > m_Settings.BruteForceLimit)
                        {
                            FireOnGuiLogNotificationOccuredEvent(string.Format("Brute force limit of {0} reached, no factors found.", m_Settings.BruteForceLimit), NotificationLevel.Warning);
                            break;
                        }

                        if (InputNumber % factor == 0)
                        {
                            // Factor found, exit gracefully
                            Factor    = factor;
                            Remainder = InputNumber / factor;
                            break;
                        }

                        if (++progressdisplay >= 100)
                        {
                            progressdisplay = 0;
                            ProgressChanged((int)((factor * 100) / limit), 100);
                        }
                    }
                }
            }

            ProgressChanged(0, 1);
        }