Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        protected override void DoExecute()
        {
            FireOnStart();

            PrimesBigInteger from = m_From;

            while (from.CompareTo(m_To) <= 0)
            {
                StringBuilder    sbMessage = new StringBuilder("[");
                PrimesBigInteger d         = PrimesBigInteger.One;
                while (d.CompareTo(from) < 0)
                {
                    if (PrimesBigInteger.GCD(d, from).Equals(PrimesBigInteger.One))
                    {
                        if (sbMessage.Length > 1)
                        {
                            sbMessage.Append(", ");
                        }
                        sbMessage.Append(d.ToString());
                        FireOnMessage(this, from, sbMessage.ToString());
                    }
                    d = d.Add(PrimesBigInteger.One);
                }
                sbMessage.Append("]");
                FireOnMessage(this, from, sbMessage.ToString());

                from = from.Add(PrimesBigInteger.One);
            }

            FireOnStop();
        }
Ejemplo n.º 3
0
        protected override void DoExecute()
        {
            FireOnStart();

            PrimesBigInteger from = m_From;

            while (from.CompareTo(m_To) <= 0)
            {
                if (from.IsPrime(20))
                {
                    FireOnMessage(this, from, from.Subtract(PrimesBigInteger.One).ToString("D"));
                }
                else
                {
                    PrimesBigInteger d       = PrimesBigInteger.One;
                    PrimesBigInteger counter = PrimesBigInteger.Zero;

                    while (d.CompareTo(from) < 0)
                    {
                        if (PrimesBigInteger.GCD(d, from).Equals(PrimesBigInteger.One))
                        {
                            counter = counter.Add(PrimesBigInteger.One);
                            FireOnMessage(this, from, counter.ToString("D"));
                        }
                        d = d.Add(PrimesBigInteger.One);
                    }

                    FireOnMessage(this, from, counter.ToString("D"));
                }

                from = from.Add(PrimesBigInteger.One);
            }

            FireOnStop();
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        private PrimesBigInteger EulerPhi(PrimesBigInteger n)
        {
            if (n.Equals(PrimesBigInteger.One))
            {
                return(PrimesBigInteger.One);
            }
            PrimesBigInteger result = PrimesBigInteger.Zero;
            PrimesBigInteger k      = PrimesBigInteger.One;

            while (k.CompareTo(n) <= 0)
            {
                if (PrimesBigInteger.GCD(k, n).Equals(PrimesBigInteger.One))
                {
                    result = result.Add(PrimesBigInteger.One);
                }
                k = k.Add(PrimesBigInteger.One);
            }

            return(result);
        }
Ejemplo n.º 6
0
        protected override void DoExecute()
        {
            FireOnStart();

            try
            {
                PrimesBigInteger modulus = m_SecondParameter;

                PrimesBigInteger from = m_From;

                while (from.CompareTo(m_To) <= 0)
                {
                    PrimesBigInteger result = PrimesBigInteger.GCD(from, modulus);
                    FireOnMessage(this, from, result.ToString("D"));
                    from = from.Add(PrimesBigInteger.One);
                }
            }
            catch (Exception ex)
            {
            }

            FireOnStop();
        }
Ejemplo n.º 7
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)
            {
            }
        }