示例#1
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();
        }
示例#2
0
        private void DrawNumberButton(PrimesBigInteger value, double x, double y)
        {
            Ellipse nb = ControlHandler.CreateObject(typeof(Ellipse)) as Ellipse;

            //NumberButton nb = ControlHandler.CreateObject(typeof(NumberButton)) as NumberButton;
            //ControlHandler.SetPropertyValue(nb, "NumberButtonStyle", NumberButtonStyle.Ellipse.ToString());
            //ControlHandler.SetPropertyValue(nb, "BINumber", value);
            ControlHandler.SetPropertyValue(nb, "Width", 6);
            ControlHandler.SetPropertyValue(nb, "Height", 6);
            ToolTip tt = ControlHandler.CreateObject(typeof(ToolTip)) as ToolTip;

            ControlHandler.SetPropertyValue(tt, "Content", value.ToString());
            ControlHandler.SetPropertyValue(nb, "ToolTip", tt);

            //if (value.IsPrime(10))
            //  ControlHandler.SetPropertyValue(nb, "Background", Brushes.Blue);
            //else
            //  ControlHandler.SetPropertyValue(nb, "Background", Brushes.Gray);
            if (value.IsPrime(10))
            {
                ControlHandler.SetPropertyValue(nb, "Fill", Brushes.Blue);
            }
            else
            {
                ControlHandler.SetPropertyValue(nb, "Fill", Brushes.Gray);
            }

            ControlHandler.ExecuteMethod(PaintArea, "SetTop", new object[] { nb, y - 3 });
            ControlHandler.ExecuteMethod(PaintArea, "SetLeft", new object[] { nb, x - 3 });
            ControlHandler.AddChild(nb, PaintArea);
        }
示例#3
0
        //private void ComputeRandom()
        //{
        //  PrimesBigInteger i = PrimesBigInteger.One;
        //  while (i.CompareTo(this.NumberOfFormulars) < 0)
        //  {
        //    PrimesBigInteger j = PrimesBigInteger.One;
        //    PrimesBigInteger to = this.NumberOfCalculations;
        //    if (NumberOfCalculations.Equals(PrimesBigInteger.NaN))
        //    {
        //      j = From;
        //      to = To;
        //    }
        //    PrimesBigInteger counter = PrimesBigInteger.Zero;
        //    PrimesBigInteger primesCounter = PrimesBigInteger.Zero;
        //    while(j.CompareTo(to)<=0){
        //      foreach (KeyValuePair<string, Range> kvp in this.Parameters)
        //      {
        //        PrimesBigInteger value = PrimesBigInteger.RandomM(kvp.Value.From.Add(kvp.Value.RangeAmount)).Add(PrimesBigInteger.One);
        //        Function.SetParameter(kvp.Key, value);
        //      }
        //      PrimesBigInteger input = j;
        //      if (!NumberOfCalculations.Equals(PrimesBigInteger.NaN))
        //      {
        //        input = PrimesBigInteger.RandomM(NumberOfCalculations);
        //      }
        //      PrimesBigInteger res = Function.Execute(input);
        //      if(res.IsPrime(10)){
        //        primesCounter = primesCounter.Add(PrimesBigInteger.One);
        //      }
        //      j = j.Add(PrimesBigInteger.One);
        //      counter = counter.Add(PrimesBigInteger.One);
        //    }
        //    if (this.FunctionResult != null) this.FunctionResult(this.Function as IPolynom, primesCounter, counter);
        //    i = i.Add(PrimesBigInteger.One);
        //  }
        //}

        private void ComputeRandom()
        {
            Random r = new Random();

            PrimesBigInteger         i             = PrimesBigInteger.One;
            PrimesBigInteger         j             = null;
            PrimesBigInteger         to            = null;
            PrimesBigInteger         counter       = null;
            PrimesBigInteger         primesCounter = null;
            IList <PrimesBigInteger> m_PrimeList   = new List <PrimesBigInteger>();

            while (i.CompareTo(this.NumberOfFormulars) <= 0)
            {
                j  = PrimesBigInteger.One;
                to = this.NumberOfCalculations;
                if (NumberOfCalculations.Equals(PrimesBigInteger.NaN))
                {
                    j  = From;
                    to = To;
                }
                counter       = PrimesBigInteger.Zero;
                primesCounter = PrimesBigInteger.Zero;
                foreach (KeyValuePair <string, Range> kvp in this.Parameters)
                {
                    PrimesBigInteger mod   = kvp.Value.To.Subtract(kvp.Value.From).Add(PrimesBigInteger.One);
                    PrimesBigInteger value = PrimesBigInteger.ValueOf(r.Next(int.MaxValue)).Mod(mod).Add(kvp.Value.From);//PrimesBigInteger.RandomM(kvp.Value.From.Add(kvp.Value.RangeAmount)).Add(PrimesBigInteger.One);
                    Function.SetParameter(kvp.Key, value);
                }
                while (j.CompareTo(to) <= 0)
                {
                    PrimesBigInteger input = j;
                    if (!NumberOfCalculations.Equals(PrimesBigInteger.NaN))
                    {
                        input = PrimesBigInteger.ValueOf(r.Next(int.MaxValue)).Mod(NumberOfCalculations);//PrimesBigInteger.RandomM(NumberOfCalculations);
                    }
                    PrimesBigInteger res = Function.Execute(input);
                    if (res.CompareTo(PrimesBigInteger.Zero) >= 0 && res.IsPrime(10))
                    {
                        if (!m_PrimeList.Contains(res))
                        {
                            m_PrimeList.Add(res);
                        }
                        primesCounter = primesCounter.Add(PrimesBigInteger.One);
                    }
                    j       = j.Add(PrimesBigInteger.One);
                    counter = counter.Add(PrimesBigInteger.One);
                }
                if (this.FunctionResult != null)
                {
                    this.FunctionResult(this.Function as IPolynom, primesCounter, PrimesBigInteger.ValueOf(m_PrimeList.Count), counter);
                }
                i = i.Add(PrimesBigInteger.One);
            }
        }
示例#4
0
        private void ComputeSystematic()
        {
            Range            rangea  = this.Parameters[0].Value;
            Range            rangeb  = this.Parameters[1].Value;
            Range            rangec  = this.Parameters[2].Value;
            PrimesBigInteger ca      = rangea.From;
            PrimesBigInteger counter = PrimesBigInteger.Zero;

            while (ca.CompareTo(rangea.To) <= 0)
            {
                m_Function.SetParameter("a", ca);
                PrimesBigInteger cb = rangeb.From;
                while (cb.CompareTo(rangeb.To) <= 0)
                {
                    m_Function.SetParameter("b", cb);
                    PrimesBigInteger cc = rangec.From;
                    while (cc.CompareTo(rangec.To) <= 0)
                    {
                        m_Function.SetParameter("c", cc);

                        PrimesBigInteger         from          = From;
                        PrimesBigInteger         primesCounter = PrimesBigInteger.Zero;
                        IList <PrimesBigInteger> m_PrimeList   = new List <PrimesBigInteger>();
                        while (from.CompareTo(To) <= 0)
                        {
                            PrimesBigInteger res = Function.Execute(from);
                            if (res.IsPrime(10))
                            {
                                if (!m_PrimeList.Contains(res))
                                {
                                    m_PrimeList.Add(res);
                                }
                                primesCounter = primesCounter.Add(PrimesBigInteger.One);
                            }
                            counter = counter.Add(PrimesBigInteger.One);
                            from    = from.Add(PrimesBigInteger.One);
                        }
                        if (this.FunctionResult != null)
                        {
                            this.FunctionResult(this.Function as IPolynom, primesCounter, PrimesBigInteger.ValueOf(m_PrimeList.Count), counter);
                        }

                        cc = cc.Add(PrimesBigInteger.One);
                    }
                    cb = cb.Add(PrimesBigInteger.One);
                }
                ca = ca.Add(PrimesBigInteger.One);
            }
        }
示例#5
0
        private void SetSextupletPrimes(PrimesBigInteger value)
        {
            if (!value.IsProbablePrime(10))
            {
                return;
            }

            PrimesBigInteger first = null;

            if (value.Equals(PrimesBigInteger.Seven))
            {
                first = PrimesBigInteger.ValueOf(11);                                        // 7 is the only prime that doesn't match the pattern, so handle this case separately
            }
            else if (!IsQuadrupletPrime(value, ref first) && !IsQuadrupletPrime(value.Add(PrimesBigInteger.Four), ref first) && !IsQuadrupletPrime(value.Subtract(PrimesBigInteger.Four), ref first))
            {
                return;
            }

            first = first.Subtract(PrimesBigInteger.Four);
            if (!first.IsPrime(10))
            {
                return;
            }
            if (!first.Add(PrimesBigInteger.ValueOf(16)).IsPrime(10))
            {
                return;
            }

            List <int> diffs = new List <int> {
                0, 4, 6, 10, 12, 16
            };
            List <PrimesBigInteger> l = new List <PrimesBigInteger>();

            foreach (var d in diffs)
            {
                PrimesBigInteger p = first.Add(PrimesBigInteger.ValueOf(d));
                l.Add(p);
                if (m_ButtonsDict.ContainsKey(p))
                {
                    MarkNumber(m_ButtonsDict[p]);
                }
            }

            lblSixTupletPrimes.Text       = string.Format(Distribution.numberline_issixtupletprime, l[0], l[1], l[2], l[3], l[4], l[5]);
            pnlSixTupletPrimes.Visibility = Visibility.Visible;
        }
        private void PrimesStatus()
        {
            ControlHandler.SetPropertyValue(btnForceAutomatic, "Visibility", Visibility.Hidden);

            StringBuilder sbResult = new StringBuilder(rsc.Primetest.soe_doneautomatic);

            if (m_Value.IsPrime(10))
            {
                sbResult.Append(string.Format(rsc.Primetest.soe_isprime, m_Value));
            }
            else
            {
                sbResult.Append(string.Format(rsc.Primetest.soe_isnotprime, m_Value));
            }

            log.Info(sbResult.ToString());
        }
        void exec_FunctionResult(PrimesBigInteger result, PrimesBigInteger input)
        {
            int row = log.NewLine();

            if (m_ExpressionExecuter.Function != null && m_ExpressionExecuter.Function.GetType().GetInterface("IPolynom") != null)
            {
                log.Info(string.Format("f({0}) = {1}", new object[] { input, result }), 0, row);
                bool isPrime = result.IsPrime(10);
                lock (counterlockobject)
                {
                    m_PolynomGeneratedCount++;
                    m_PolynomGeneratedPrimes += (isPrime) ? 1 : 0;
                }
                log.Info((isPrime) ? Primes.Resources.lang.WpfControls.Generation.PrimesGeneration.isPrime : Primes.Resources.lang.WpfControls.Generation.PrimesGeneration.isNotPrime, 1, row);
            }
            else
            {
                log.Info(result.ToString(), 0, row);
            }
        }
示例#8
0
        private void SetTwinPrimes(PrimesBigInteger value)
        {
            PrimesBigInteger twin1 = value.Subtract(PrimesBigInteger.One);
            PrimesBigInteger twin2 = value.Add(PrimesBigInteger.One);
            PrimesBigInteger tmp   = null;

            if (twin1.IsPrime(20) && twin2.IsPrime(20))
            {
                lblTwinPrimes.Text       = string.Format(Distribution.numberline_insidetwinprime, value, twin1, twin2);
                lblTwinPrimes.Visibility = Visibility.Visible;
            }
            else if (value.IsTwinPrime(ref tmp))
            {
                twin1 = PrimesBigInteger.Min(value, tmp);
                twin2 = PrimesBigInteger.Max(value, tmp);
                if (m_ButtonsDict.ContainsKey(twin1))
                {
                    MarkNumber(m_ButtonsDict[twin1]);
                }
                if (m_ButtonsDict.ContainsKey(twin2))
                {
                    MarkNumber(m_ButtonsDict[twin2]);
                }
                lblTwinPrimes.Text       = string.Format(Distribution.numberline_istwinprime, twin1, twin2);
                lblTwinPrimes.Visibility = Visibility.Visible;
            }

            PrimesBigInteger a    = null;
            PrimesBigInteger b    = null;
            string           text = "";

            twin1.PriorTwinPrime(ref a, ref b);
            if (a.CompareTo(twin1) < 0)
            {
                text = string.Format(Distribution.numberline_priortwinprime, a, b) + " ";
            }
            twin1.Add(PrimesBigInteger.One).NextTwinPrime(ref a, ref b);
            text += string.Format(Distribution.numberline_nexttwinprime, a, b);

            lblTwinPrimes2.Text = text;
        }
示例#9
0
        private void DoFactorize(object o)
        {
            if (o == null)
            {
                return;
            }

            Dictionary <PrimesBigInteger, long> factors = null;
            PrimesBigInteger value = null;

            if (o.GetType() == typeof(PrimesBigInteger))
            {
                ControlHandler.SetPropertyValue(lblCalcFactorizationInfo, "Text", Distribution.numberline_factorizationcalculating);
                value   = o as PrimesBigInteger;
                factors = value.Factorize();
            }
            else if (o.GetType() == typeof(Dictionary <PrimesBigInteger, long>))
            {
                factors = o as Dictionary <PrimesBigInteger, long>;
                value   = PrimesBigInteger.Refactor(factors);
            }

            if (factors != null)
            {
                String s = value.ToString();
                if (!value.IsPrime(20) && !value.Equals(PrimesBigInteger.One))
                {
                    s += " = " + String.Join(" * ", factors.Keys.Select(i => i + ((factors[i] > 1) ? "^" + factors[i] : "")).ToArray());
                }

                ControlHandler.SetPropertyValue(lblFactors, "Visibility", Visibility.Visible);
                ControlHandler.SetPropertyValue(lblFactors, "Text", s);
            }

            if (FactorizationDone != null)
            {
                FactorizationDone();
            }
        }
示例#10
0
        private void Execute(bool doExecute)
        {
            ClearLog();
            intervals.Clear();

            string _input = tbInput.Text;

            if (!string.IsNullOrEmpty(_input))
            {
                string[] input = _input.Trim().Split(',');
                if (input != null && input.Length > 0)
                {
                    foreach (string s in input)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            string[] _inputrange = s.Split(':');
                            if (_inputrange.Length == 1)
                            {
                                PrimesBigInteger ipt = null;
                                validator.Value = s;
                                Primes.WpfControls.Validation.ValidationResult res = validator.Validate(ref ipt);
                                if (res == Primes.WpfControls.Validation.ValidationResult.OK)
                                {
                                    if (ipt.IsPrime(10))
                                    {
                                        intervals.Add(new List <PrimesBigInteger> {
                                            ipt, ipt
                                        });
                                        if (ipt.CompareTo(MAX) > 0)
                                        {
                                            log.Info(string.Format(rsc.proot_warningbignumber, s));
                                        }
                                    }
                                    else
                                    {
                                        log.Info(string.Format(rsc.proot_noprime, s));
                                    }
                                }
                                else
                                {
                                    log.Info(string.Format(rsc.proot_novalidnumber, new object[] { s, MIN.ToString(), MAX.ToString() }));
                                }
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(_inputrange[0]) && string.IsNullOrEmpty(_inputrange[1]))
                                {
                                    log.Info(rsc.proot_rangeboth);
                                }
                                else if (string.IsNullOrEmpty(_inputrange[0]))
                                {
                                    log.Info(string.Format(rsc.proot_rangeupper, _inputrange[1]));
                                }
                                else if (string.IsNullOrEmpty(_inputrange[1]))
                                {
                                    log.Info(string.Format(rsc.proot_rangedown, _inputrange[0]));
                                }
                                else
                                {
                                    PrimesBigInteger i1 = IsGmpBigInteger(_inputrange[0]);
                                    PrimesBigInteger i2 = IsGmpBigInteger(_inputrange[1]);
                                    if (i1 != null && i2 != null)
                                    {
                                        if (i1.CompareTo(i2) >= 0)
                                        {
                                            log.Info(string.Format(rsc.proot_wronginterval, s));
                                        }
                                        else
                                        {
                                            intervals.Add(new List <PrimesBigInteger> {
                                                i1, i2
                                            });
                                            if (i2.CompareTo(MAXWARN) > 0)
                                            {
                                                log.Info(string.Format(rsc.proot_warningbiginterval, s));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (doExecute && intervals.Count > 0)
                {
                    StartThread();
                }
            }
            else
            {
                Info(rsc.proot_insert);
            }
        }
示例#11
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)
            {
            }
        }