예제 #1
0
        private PrimesBigInteger CalculateFactor(PrimesBigInteger value)
        {
            PrimesBigInteger x = m_StartFX;
            PrimesBigInteger y = m_StartFX;
            PrimesBigInteger d = PrimesBigInteger.One;
            PrimesBigInteger a = m_A;
            int i = 0;

            if (value.Mod(PrimesBigInteger.Two).Equals(PrimesBigInteger.Zero))
            {
                return(PrimesBigInteger.Two);
            }
            do
            {
                x = x.ModPow(PrimesBigInteger.Two, value).Add(a).Mod(value);
                y = y.ModPow(PrimesBigInteger.Two, value).Add(a).Mod(value);
                y = y.ModPow(PrimesBigInteger.Two, value).Add(a).Mod(value);
                d = PrimesBigInteger.GCD(x.Subtract(y), value);
                i++;
                if (y.Equals(x))
                {
                    log.Info("Change Values");
                    a = PrimesBigInteger.ValueOf(new Random().Next());
                    x = y = PrimesBigInteger.ValueOf(new Random().Next());
                    i = 0;
                }
            }while (d.Equals(PrimesBigInteger.One));
            return(d);
        }
예제 #2
0
        private bool IsPrimitiveRoot(PrimesBigInteger root, PrimesBigInteger prime)
        {
            if (!PrimesBigInteger.GCD(root, prime).Equals(PrimesBigInteger.One))
            {
                return(false);
            }

            PrimesBigInteger primeMinus1 = prime.Subtract(PrimesBigInteger.One);
            PrimesBigInteger k           = PrimesBigInteger.One;

            while (k.CompareTo(primeMinus1) < 0)
            {
                if (m_Jump)
                {
                    return(false);
                }
                if (root.ModPow(k, prime).Equals(PrimesBigInteger.One))
                {
                    return(false);
                }
                k = k.Add(PrimesBigInteger.One);
            }

            return(true);
        }
예제 #3
0
        private bool Witness(PrimesBigInteger a)
        {
            // a mustn't be a multiple of n
            if (a.Mod(m_Value).CompareTo(0) == 0)
            {
                return(false);
            }

            PrimesBigInteger n_1 = m_Value - 1;

            log.Info(string.Format("n-1 = {0} = 2^{1} * {2}", n_1, m_shift, m_d));

            PrimesBigInteger former = a.ModPow(m_d, m_Value);

            log.Info(string.Format(rsc.Primetest.mr_calculating1, a, m_d, m_Value, former));
            if (former.CompareTo(1) == 0)
            {
                log.Info(string.Format(rsc.Primetest.mr_isprime, a, m_Value));
                return(false);
            }

            PrimesBigInteger square = 1;

            for (int i = 1; i <= m_shift; i++)
            {
                square = former.ModPow(2, m_Value);
                log.Info(string.Format(rsc.Primetest.mr_calculating2, a, m_d, 1 << i, m_Value, former, square));
                if (square.CompareTo(1) == 0)
                {
                    bool trivialroot = former.CompareTo(1) == 0 || former.CompareTo(n_1) == 0;
                    if (trivialroot)
                    {
                        log.Info(string.Format(rsc.Primetest.mr_isprime, a, m_Value));
                        return(false);
                    }
                    else
                    {
                        log.Info(string.Format(rsc.Primetest.mr_isnotprime1, former, m_Value));
                        return(true);
                    }
                }
                former = square;
            }

            log.Info(string.Format(rsc.Primetest.mr_isnotprime2, m_Value, a, n_1, m_Value, square));
            return(true);
        }
예제 #4
0
        private bool ExecuteLog(PrimesBigInteger a)
        {
            PrimesBigInteger result = a.ModPow(m_Value.Subtract(PrimesBigInteger.One), m_Value);

            log.Info(
                string.Format(
                    "Berechne {0}^{1} mod {2} = {3}",
                    new object[] { a.ToString(), m_Value.Subtract(PrimesBigInteger.One), m_Value.ToString(), result.ToString() }));
            ControlHandler.SetPropertyValue(lblA, "Content", a.ToString());
            ControlHandler.SetPropertyValue(lblExp, "Text", m_Value.ToString() + "-1");
            ControlHandler.SetPropertyValue(lblP, "Content", m_Value.ToString());
            ControlHandler.SetPropertyValue(lblCalc, "Text", result.ToString());

            if (result.Equals(PrimesBigInteger.One))
            {
                log.Info(string.Format("{0} hat den Fermattest bestanden und ist mir einer Wahrscheinlichkeit von 50% eine Primzahl", m_Value.ToString()));
            }
            else
            {
                log.Info(string.Format("{0} hat den Fermattest nicht bestanden und ist damit definitiv keine Primzahl. {1} ist Belastungszeuge gegen {2}", new object[] { m_Value.ToString(), a.ToString(), m_Value.ToString() }));
            }

            return(result.Equals(PrimesBigInteger.One));
        }
예제 #5
0
        private void DoCalculatePrimitiveRoots()
        {
            try
            {
                DateTime start = DateTime.Now;

                FireOnStart();

                m_Jump = false;

                int numberOfPrimes = 0;

                foreach (var interval in intervals)
                {
                    PrimesBigInteger prime = interval[0];
                    if (!prime.IsPrime(10))
                    {
                        prime = prime.NextProbablePrime();
                    }
                    for (; prime.CompareTo(interval[1]) <= 0; prime = prime.NextProbablePrime())
                    {
                        numberOfPrimes++;

                        int row1 = log.NewLine();
                        int row2 = log.NewLine();

                        log.Info(string.Format(rsc.proot_calculating, prime.ToString()), 0, row1);

                        PrimesBigInteger primeMinus1 = prime.Subtract(PrimesBigInteger.One);
                        PrimesBigInteger numroots    = primeMinus1.Phi();

                        string fmt    = numroots.CompareTo(PrimesBigInteger.One) == 0 ? rsc.proot_resultcalc : rsc.proot_resultscalc;
                        string result = string.Format(fmt, prime.ToString(), numroots.ToString());
                        log.Info(result + ". " + rsc.proot_calculating, 0, row1);

                        PrimesBigInteger primitiveroot = PrimesBigInteger.One;
                        while (primitiveroot.CompareTo(prime) < 0)
                        {
                            if (m_Jump)
                            {
                                break;
                            }
                            if (IsPrimitiveRoot(primitiveroot, prime))
                            {
                                break;
                            }
                            primitiveroot = primitiveroot.Add(PrimesBigInteger.One);
                        }

                        List <PrimesBigInteger> roots = new List <PrimesBigInteger>();

                        PrimesBigInteger i       = PrimesBigInteger.One;
                        bool             skipped = false;

                        while (i.CompareTo(prime) < 0)
                        {
                            lock (m_JumpLockObject)
                            {
                                if (m_Jump)
                                {
                                    m_Jump  = false;
                                    skipped = true;
                                    break;
                                }
                            }
                            if (PrimesBigInteger.GCD(i, primeMinus1).Equals(PrimesBigInteger.One))
                            {
                                roots.Add(primitiveroot.ModPow(i, prime));
                            }
                            i = i.Add(PrimesBigInteger.One);
                        }

                        if (skipped)
                        {
                            log.Info(result + ". " + rsc.proot_skip, 0, row1);
                        }
                        else
                        {
                            log.Info(result + ". " + rsc.proot_printing, 0, row1);
                            roots.Sort(PrimesBigInteger.Compare);
                            //string numbers = string.Join(" ", roots.ToArray().Select(x => x.ToString()));
                            StringBuilder sb = new StringBuilder();
                            foreach (var r in roots)
                            {
                                lock (m_JumpLockObject)
                                {
                                    if (m_Jump)
                                    {
                                        m_Jump  = false;
                                        skipped = true;
                                        break;
                                    }
                                }
                                sb.Append(r.ToString() + " ");
                            }
                            if (skipped)
                            {
                                log.Info(result + ". " + rsc.proot_skip, 0, row1);
                            }
                            else
                            {
                                string numbers = sb.ToString();
                                log.Info(numbers, 0, row2);
                                log.Info(result + ":", 0, row1);
                            }
                        }

                        log.NewLine();
                    }
                }

                if (numberOfPrimes == 0)
                {
                    log.Info(rsc.proot_noprimes);
                }

                TimeSpan diff = DateTime.Now - start;

                StopThread();
            }
            catch (Exception ex)
            {
            }
        }
예제 #6
0
        private void DoExecuteGraphic(PrimesBigInteger a)
        {
            lock (m_RunningLockObject)
            {
                m_Running = true;
            }
            Point            lastPoint = new Point(-1, -1);
            PrimesBigInteger factor    = null;

            ControlHandler.SetPropertyValue(lblA, "Content", a.ToString());
            ControlHandler.SetPropertyValue(lblExp, "Text", m_Value.ToString() + "-1");
            ControlHandler.SetPropertyValue(lblP, "Content", m_Value.ToString());

            ControlHandler.SetPropertyValue(lblCalc, "Text", string.Empty);
            ControlHandler.SetPropertyValue(lblCalc, "Text", a.ModPow(m_Value.Subtract(PrimesBigInteger.One), m_Value).ToString());
            PrimesBigInteger i = PrimesBigInteger.Two;

            PrimesBigInteger result  = null;
            PrimesBigInteger counter = PrimesBigInteger.Zero;

            while (i.CompareTo(m_Value.Subtract(PrimesBigInteger.One)) <= 0)
            {
                Thread.Sleep(100);
                result = a.ModPow(i, m_Value);
                log.Info(
                    string.Format(
                        "Berechne {0}^{1} mod {2} = {3}",
                        new object[] { a.ToString(), i.ToString(), m_Value.ToString(), result.ToString() }));

                if (factor == null)
                {
                    factor = result;
                }
                else
                {
                    factor = factor.Multiply(result);
                }
                //StringBuilder sbText = new StringBuilder();// new StringBuilder(ControlHandler.GetPropertyValue(lblCalc, "Text") as string);
                //sbText.Append(result.ToString());
                //if (i.CompareTo(m_Value.Subtract(PrimesBigInteger.One)) < 0)
                //  sbText.Append(" * ");
                //ControlHandler.SetPropertyValue(lblCalc, "Text", sbText.ToString());

                if (lastPoint.X == -1 && lastPoint.Y == -1)
                {
                    lastPoint = m_Points[result.IntValue];
                }
                else
                {
                    Point newPoint = m_Points[result.IntValue];
                    CreateArrow(counter, lastPoint, newPoint);
                    lastPoint = newPoint;
                }
                i       = i.Add(PrimesBigInteger.One);
                counter = counter.Add(PrimesBigInteger.One);
            }

            if (result != null)
            {
                if (result.Equals(PrimesBigInteger.One))
                {
                    log.Info(
                        string.Format(
                            "Berechne {0}^{1} mod {2} = 1. {3} könnte eine Primzahl sein.",
                            new object[] { a.ToString(), m_Value.Subtract(PrimesBigInteger.One).ToString(), m_Value.ToString(), m_Value.ToString() }));
                }
                else
                {
                    log.Info(
                        string.Format(
                            "Berechne {0}^{1} mod {2} = {3}. {4} ist damit definitiv keine Primzahl.",
                            new object[] { a.ToString(), m_Value.Subtract(PrimesBigInteger.One).ToString(), m_Value.ToString(), result.ToString(), m_Value.ToString() }));
                }
            }

            if (CancelTest != null)
            {
                CancelTest();
            }

            lock (m_RunningLockObject)
            {
                m_Running = false;
            }
        }