Esempio n. 1
0
        //public override bool Execute(ref QSData data)
        //{
        //  base.Execute(ref data);

        //  long sqrt = (long)Math.Floor(Math.Sqrt(m_QSData.N)) + 1;
        //  ControlHandler.SetPropertyValue(
        //    m_lblInfo,
        //    "Text",
        //    "Die Quadratwurzel aus " + m_QSData.N.ToString("D") + " ist " + Math.Sqrt(m_QSData.N).ToString("N") + " ≈ " + sqrt);
        //  int counter = 0;

        //  for (long i = data.From; i <= data.To; i++)
        //  {
        //    ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
        //    string a = StringFormat.FormatDoubleToIntString((sqrt + i)) + "²";
        //    string aminus1 = StringFormat.FormatDoubleToIntString((Math.Pow((sqrt + i), 2) - m_QSData.N));

        //    data.Add(new QuadraticPair(sqrt + i, ((long)Math.Pow((sqrt + i), 2) - m_QSData.N)));
        //    ControlHandler.ExecuteMethod(
        //      this,
        //      "AddToGrid",
        //      new object[] { Grid, a, counter + 1, 0, 0, 0 });
        //    ControlHandler.ExecuteMethod(
        //      this,
        //      "AddToGrid",
        //      new object[] { Grid, aminus1, counter + 1, 1, 0, 0 });

        //    counter++;
        //    Thread.Sleep(m_Delay);

        //  }

        //  return true;
        //}

        public override QSResult Execute(ref QSData data)
        {
            base.Execute(ref data);

            long sqrt = (long)Math.Ceiling(Math.Sqrt(m_QSData.N));

            String msg = String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step1_squareroot, m_QSData.N, Math.Sqrt(m_QSData.N).ToString("N"), sqrt, data.From, data.To);

            ControlHandler.SetPropertyValue(m_lblInfo, "Text", msg);

            int counter = 2;

            for (long i = data.From; i <= data.To; i++)
            {
                int a = (int)i;
                int b = (int)(a * a - m_QSData.N);
                data.Add(new QuadraticPair(a, b));

                ControlHandler.AddColumnDefintion(Grid, 1, GridUnitType.Auto);
                base.AddToGrid(Grid, a.ToString() + "²", 0, counter);
                base.AddToGrid(Grid, b.ToString(), 1, counter);

                counter++;
                //Thread.Sleep(m_Delay);
            }

            return(QSResult.Ok);
        }
Esempio n. 2
0
        public QuadraticSieveControl()
        {
            InitializeComponent();

            m_State = QSResult.Ok;

            stepwise   = false;
            resetEvent = new ManualResetEvent(false);
            m_Count    = 10;
            m_Step1    = new Step1(gridFirstStep, lblInfoStep1);

            m_Step2 = new Step2(gridSecondStep, lblInfoStep2);
            m_Step2.PreStep();

            m_Step3 = new Step3(gridThirdStep, lblInfoStep3);
            m_Step3.PreStep();

            m_Step4              = new Step4(gridFourthStep, lblInfoStep4);
            m_Step4.FoundFactor += new FoundFactor(m_Step4_FoundFactor);
            m_Step4.PreStep();

            m_Data    = new QSData();
            m_Factors = new Dictionary <PrimesBigInteger, PrimesBigInteger>();
            //m_Data.From = -41;
            //m_Data.To = 41;
        }
Esempio n. 3
0
        public override QSResult Execute(ref QSData data)
        {
            IList <int>   m_Factors = data.CalculateFactorBase();
            StringBuilder fb        = new StringBuilder();

            foreach (int i in m_Factors)
            {
                if (fb.Length > 0)
                {
                    fb.Append(",");
                }
                fb.Append(i);
            }

            ControlHandler.SetPropertyValue(
                m_lblInfo,
                "Text",
                string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step2_B, data.B, fb.ToString()));

            IList <long> list    = new List <long>();
            int          counter = 1;

            foreach (QuadraticPair pair in data)
            {
                ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);

                AddToGrid(Grid, pair.A.ToString(), counter, 0, 0, 0);
                AddToGrid(Grid, pair.B.ToString(), counter, 1, 0, 0);
                TextBlock     tb = AddTextBlock(counter, 2);
                StringBuilder sb = new StringBuilder();

                long b = pair.B;
                if (b < 0)
                {
                    b = -b;
                    pair.AddExponent(-1, 1);
                    sb.Append("-1");
                }

                if (b != 0)
                {
                    for (int i = 0; i < m_Factors.Count; i++)
                    {
                        int f   = m_Factors[i];
                        int exp = 0;
                        while (b % f == 0)
                        {
                            b /= f;
                            exp++;
                        }
                        if (exp > 0)
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(" * ");
                            }
                            sb.Append(f);
                            if (exp > 1)
                            {
                                sb.Append("^" + exp);
                            }
                        }
                        pair.AddExponent(f, exp);
                    }
                }

                if (b > 1 && sb.Length > 0)
                {
                    sb.Append(" * " + b);
                }
                ControlHandler.SetPropertyValue(tb, "Text", sb.ToString());

                pair.IsBSmooth = (b == 1) || (b == 0);
                string s = pair.IsBSmooth ? Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step2_yes : Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step2_no;
                ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, s, counter++, 3, 0, 0 });

                Thread.Sleep(m_Delay);
            }

            return(QSResult.Ok);
        }
Esempio n. 4
0
        public override QSResult Execute(ref QSData data)
        {
            int counter = 0;
            IList <QuadraticPair> pairs = data.BSmooth;
            bool foundquadratic         = false;

            ControlHandler.SetPropertyValue(
                m_lblInfo,
                "Text",
                string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_smooth, pairs.Count));

            //
            // Suche nach glatten Werten, die selbst schon Quadrat sind
            //

            foreach (QuadraticPair pair in pairs)
            {
                String msg;

                if (data.IsIgnored(PrimesBigInteger.ValueOf(pair.B)))
                {
                    pair.QuadraticStatus = QuadraticStatus.Ignore;
                }

                if (pair.QuadraticStatus == QuadraticStatus.Ignore)
                {
                    msg = String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_ignored, pair.B);
                }
                else
                {
                    int sqrt = (int)Math.Sqrt(pair.B);
                    if (sqrt * sqrt == pair.B)
                    {
                        foundquadratic       = true;
                        pair.QuadraticStatus = QuadraticStatus.Quadratic;
                        msg = String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_issquare, pair.B, pair.B, sqrt);
                    }
                    else
                    {
                        msg = String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_isnotsquare, pair.B);
                    }
                }

                ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, msg.ToString(), counter++, 0, 0, 0 });

                if (foundquadratic)
                {
                    return(QSResult.Ok);
                }
            }

            //
            // Suche nach Produkten von glatten Werten, die ein Quadrat ergeben
            //

            int pslen = (int)Math.Pow(2, pairs.Count);

            for (int i = 1; i < pslen; i++)
            {
                MyInteger mi = new MyInteger(i);
                if (mi.BitCount <= 1)
                {
                    continue;
                }

                StringBuilder msg     = new StringBuilder();
                int[]         indices = mi.GetIndices();
                long          erg     = 1;

                foreach (int j in indices)
                {
                    if (msg.Length > 0)
                    {
                        msg.Append(" * ");
                    }
                    msg.Append(pairs[j].B);
                    erg *= pairs[j].B;
                }

                if (erg != 1)
                {
                    if (data.IsIgnored(PrimesBigInteger.ValueOf(erg)))
                    {
                        ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                        TextBlock tb = AddTextBlock(counter++, 0);
                        ControlHandler.SetPropertyValue(tb, "Text", Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombi + msg.ToString());
                        ControlHandler.SetPropertyValue(tb, "Text", string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombiignore, msg, erg));
                    }
                    else
                    {
                        int sqrt = (int)Math.Sqrt(erg);
                        if (sqrt * sqrt == erg)
                        {
                            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                            TextBlock tb = AddTextBlock(counter++, 0);
                            ControlHandler.SetPropertyValue(tb, "Text", Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombi + msg.ToString());
                            ControlHandler.SetPropertyValue(tb, "Text", string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombiisquare, msg, erg, erg, sqrt));
                            foreach (int j in indices)
                            {
                                pairs[j].QuadraticStatus = QuadraticStatus.Part;
                            }
                            return(QSResult.Ok);
                        }
                        else
                        {
                            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                            TextBlock tb = AddTextBlock(counter++, 0);
                            ControlHandler.SetPropertyValue(tb, "Text", Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombi + msg.ToString());
                            ControlHandler.SetPropertyValue(tb, "Text", string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step3_testcombiisnotsquare, msg, erg));
                        }
                    }
                }

                //Thread.Sleep(m_Delay);
            }

            return(QSResult.Failed); // keine Quadrate gefunden
        }
Esempio n. 5
0
 public virtual QSResult Execute(ref QSData data)
 {
     this.m_QSData = data;
     return(QSResult.Ok);
 }
Esempio n. 6
0
        public override QSResult Execute(ref QSData data)
        {
            int counter = 0;
            IList <QuadraticPair> smoothpair = data.BSmooth;
            long   productA = 1;
            long   productB = 1;
            String msg;

            foreach (QuadraticPair pair in smoothpair)
            {
                if (pair.QuadraticStatus == QuadraticStatus.Quadratic)
                {
                    productA = pair.A;
                    productB = pair.B;
                    break;
                }
            }

            if (productA == 1 && productB == 1)
            {
                foreach (QuadraticPair pair in smoothpair)
                {
                    if (pair.QuadraticStatus == QuadraticStatus.Part)
                    {
                        productA             = (productA * pair.A) % data.N;
                        productB            *= pair.B;
                        pair.QuadraticStatus = QuadraticStatus.Nope;
                    }
                }
            }

            if (productA == 1 || productB == 1)
            {
                ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_end, data.N), counter++, 0, 0, 0 });
                return(QSResult.Ok);
            }

            long sqb = (long)Math.Sqrt(productB);

            StringBuilder sbInfo = new StringBuilder();

            sbInfo.Append(string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_abcalculated, productA, sqb));
            sbInfo.Append("\n");
            sbInfo.Append(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_checkcong);
            ControlHandler.SetPropertyValue(m_lblInfo, "Text", sbInfo.ToString());

            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, BuildQuadMod(productA, sqb, data.N), counter++, 0, 0, 0 });
            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, BuildMod(productA, sqb, data.N), counter++, 0, 0, 0 });

            if (!ModuloTest(productA, sqb, data.N))    // Modulotest nicht bestanden
            {
                data.AddIgnoreQuadrat(PrimesBigInteger.ValueOf(productB));
                msg = Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_notproofed;
                ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, msg, counter++, 0, 0, 0 });

                return(QSResult.Failed);
            }

            msg = String.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_proofed, data.N);
            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, msg, counter++, 0, 0, 0 });

            long factor1 = (long)BigInteger.GreatestCommonDivisor(productA + sqb, data.N);
            long factor2 = (long)BigInteger.GreatestCommonDivisor(productA - sqb, data.N);

            bool trivialfactor1 = (factor1 == 1 || factor1 == data.N);
            bool trivialfactor2 = (factor2 == 1 || factor2 == data.N);

            String sbfactor1 = string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_factor1, productA, sqb, data.N, factor1);

            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, sbfactor1, counter++, 0, 0, 0 });

            String sbfactor2 = string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_factor2, productA, sqb, data.N, factor2);

            ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, sbfactor2, counter++, 0, 0, 0 });

            if (trivialfactor1 && trivialfactor2)
            {
                data.AddIgnoreQuadrat(PrimesBigInteger.ValueOf(productB));
                msg = string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_reset, productA, sqb);
                ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
                ControlHandler.ExecuteMethod(this, "AddToGrid", new object[] { Grid, msg, counter++, 0, 0, 0 });

                return(QSResult.Failed);
            }

            long nontrivialfactor = trivialfactor1 ? factor2 : factor1;

            FireFoundFactorEvent(PrimesBigInteger.ValueOf(nontrivialfactor));
            FireFoundFactorEvent(PrimesBigInteger.ValueOf(data.N / nontrivialfactor));

            //Boolean p1 = BigIntegerHelper.IsProbablePrime(factor1);
            //Boolean p2 = BigIntegerHelper.IsProbablePrime(factor2);

            //if (p1) FireFoundFactorEvent(PrimesBigInteger.ValueOf(factor1));
            //if (p2) FireFoundFactorEvent(PrimesBigInteger.ValueOf(factor2));

            //if( !(p1 && p2) ) // falls ein Faktor nicht prim ist
            //{
            //    long notPrime = p1 ? factor2 : factor1;
            //    msg = string.Format(Primes.Resources.lang.WpfControls.Factorization.Factorization.qs_step4_refactorize, notPrime, notPrime);
            //    ControlHandler.AddRowDefintion(Grid, 1, GridUnitType.Auto);
            //    ControlHandler.ExecuteMethod( this, "AddToGrid", new object[] { Grid, msg, counter++, 0, 0, 0 });

            //    data.N = notPrime;
            //    return QSResult.Restart;
            //}

            return(QSResult.Ok);
        }