예제 #1
0
        //public event VoidDelegate Finshed;

        #endregion

        #region Work

        protected override void OnDoWork()
        {
            m_Finished.Reset();
            PrimesBigInteger value = PrimesBigInteger.Two;

            while (value.CompareTo(m_Max) <= 0)
            {
                m_Primes.Add(value);
                value = value.NextProbablePrime();
            }
            m_Finished.Set();
        }
예제 #2
0
        public IList <int> CalculateFactorBase()
        {
            List <int>       result = new List <int>();
            PrimesBigInteger b      = PrimesBigInteger.ValueOf(m_B);
            PrimesBigInteger c      = PrimesBigInteger.Two;

            while (c.CompareTo(b) <= 0)
            {
                result.Add(c.IntValue);
                c = c.NextProbablePrime();
            }
            return(result);
        }
예제 #3
0
        void StepAutomatic()
        {
            StepResult       result = StepResult.SUCCESS;
            PrimesBigInteger value  = s.Expected;

            while (result != StepResult.END)
            {
                log.Info(string.Format(rsc.Primetest.soe_removemultipleof, value));
                result = s.DoStep(value);

                value = value.NextProbablePrime();
            }
            PrimesStatus();
            sievegrid.MarkNumbers(Brushes.LightSkyBlue);
            FireOnCancel();
            m_ThreadAutomatic.Abort();
        }
예제 #4
0
 public StepResult DoStep(PrimesBigInteger value)
 {
     if (m_Expected.CompareTo(value) == 0)
     {
         m_Numbergrid.RemoveMulipleOf(value);
         m_Expected = m_Expected.NextProbablePrime();
         m_Current  = value;
         if (m_Current.Pow(2).CompareTo(m_MaxValue) >= 0)
         {
             return(StepResult.END);
         }
         return(StepResult.SUCCESS);
     }
     else
     {
         return(StepResult.FAILED);
     }
 }
예제 #5
0
        private void TrialDivision()
        {
            m_Height = 0;
            PrimesBigInteger divisor = PrimesBigInteger.Two;

            FireOnActualDivisorChanged(divisor);
            PrimesBigInteger  value = new PrimesBigInteger(this.m_Root.Value);
            GmpFactorTreeNode node  = this.m_Root;

            while (!value.IsProbablePrime(10))
            {
                //int counter = 0;
                while (value.Mod(divisor).CompareTo(PrimesBigInteger.Zero) != 0)
                {
                    divisor = divisor.NextProbablePrime();
                    FireOnActualDivisorChanged(divisor);
                    //counter++;
                    //if (counter == 1000000)
                    //{
                    //  if (OnCanceled != null) OnCanceled("Nach 100.000 Versuchen wurde kein weiterer Faktor gefunden, das Verfahren wird abgebrochen.");
                    //  CancelFactorize();
                    //}
                }
                value       = value.Divide(divisor);
                m_Remainder = value;
                GmpFactorTreeNode primeNodeTmp = new GmpFactorTreeNode(divisor);
                primeNodeTmp.IsPrime = true;
                node.AddChild(primeNodeTmp);
                node         = node.AddChild(new GmpFactorTreeNode(value));
                node.IsPrime = value.IsProbablePrime(20);
                m_Height++;
                AddFactor(divisor);
            }
            m_Remainder = null;

            node.IsPrime = true;
            AddFactor(node.Value);
            if (OnStop != null)
            {
                OnStop();
            }
        }
예제 #6
0
        public PrimesBigInteger Execute(PrimesBigInteger input)
        {
            int l = m_Length.IntValue;

            // try to find a random prime in the range
            for (int i = 0; i < 2; i++)
            {
                m_LastPrime = GetStartBigInteger();
                if (m_LastPrime.ToString().Length != l) continue;
                if (!m_GeneratedPrimes.Contains(m_LastPrime))
                {
                    m_GeneratedPrimes.Add(m_LastPrime);
                    return m_LastPrime;
                }
            }

            // if that fails, try to find a prime systematically from the start of the range
            StringBuilder r = new StringBuilder("1");
            for (int i = 1; i < l; i++) r.Append("0");
            m_LastPrime = new PrimesBigInteger(r.ToString());

            for (int i = 0; i < 1000; i++)
            {
                m_LastPrime = m_LastPrime.NextProbablePrime();
                if (m_LastPrime.ToString().Length != l) break;
                if (!m_GeneratedPrimes.Contains(m_LastPrime))
                {
                    m_GeneratedPrimes.Add(m_LastPrime);
                    return m_LastPrime;
                }
            }

            if (NonFurtherPrimeFound != null) NonFurtherPrimeFound();

            return m_LastPrime;
        }
예제 #7
0
 private void SetNeighborPrimes(PrimesBigInteger value)
 {
     lblNextPrime.Text  = value.NextProbablePrime().ToString("D");
     lblPriorPrime.Text = value.PriorProbablePrime(true).ToString("D");
 }
예제 #8
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)
            {
            }
        }