public void Execute(PrimesBigInteger value) { CancelTestThread(); log.Clear(); m_Value = value; if (m_Value.Mod(2).CompareTo(0) == 0) { log.Info(string.Format(rsc.Primetest.mr_iseven, value)); FireEventCancelTest(); } else { log.Info(string.Format(rsc.Primetest.mr_shiftcalc, m_Value - 1)); GetShift(value); log.Info(string.Format((m_shift == 1) ? rsc.Primetest.mr_shiftedright : rsc.Primetest.mr_shiftedright2, m_shift, m_Value - 1, m_d)); log.Info(""); switch (KindOfTest) { case KOD.Random: ExecuteRandom(); break; case KOD.Systematic: ExecuteSystematic(); break; } } }
/// <summary> /// Calculates binary expressions and pushes the result into the operands stack /// </summary> /// <param name="op">Binary operator</param> /// <param name="operand1">First operand</param> /// <param name="operand2">Second operand</param> private void Calculate(string op, PrimesBigInteger operand1, PrimesBigInteger operand2) { PrimesBigInteger res = PrimesBigInteger.Zero; try { switch (op) { case Token.Add: res = operand1.Add(operand2); break; case Token.Subtract: res = operand1.Subtract(operand2); break; case Token.Multiply: res = operand1.Multiply(operand2); break; case Token.Divide: res = operand1.Divide(operand2); break; case Token.Mod: res = operand1.Mod(operand2); break; case Token.Power: res = operand1.Pow(operand2.IntValue); break; case Token.Log: res = PrimesBigInteger.Zero; break; case Token.Root: res = PrimesBigInteger.Zero; break; } operands.Push(PostProcess(res)); } catch (Exception e) { ThrowException(e.Message); } }
protected override void DoExecute() { FireOnStart(); PrimesBigInteger from = m_From; while (from.CompareTo(m_To) <= 0) { PrimesBigInteger result = PrimesBigInteger.Zero; PrimesBigInteger k = PrimesBigInteger.One; while (k.CompareTo(from) <= 0) { if (from.Mod(k).Equals(PrimesBigInteger.Zero)) { PrimesBigInteger phik = EulerPhi(k); result = result.Add(phik); FireOnMessage(this, from, result.ToString()); } k = k.Add(PrimesBigInteger.One); } FireOnMessage(this, from, result.ToString()); from = from.Add(PrimesBigInteger.One); } FireOnStop(); }
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.Multiply(PrimesBigInteger.Two).CompareTo(from) <= 0) { if (from.Mod(d).Equals(PrimesBigInteger.Zero)) { if (sbMessage.Length > 1) { sbMessage.Append(", "); } sbMessage.Append(d.ToString()); FireOnMessage(this, from, sbMessage.ToString()); } d = d.Add(PrimesBigInteger.One); } sbMessage.Append(", " + from.ToString() + "]"); FireOnMessage(this, from, sbMessage.ToString()); from = from.Add(PrimesBigInteger.One); } FireOnStop(); }
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); }
//private bool MillerRabin(PrimesBigInteger rounds, PrimesBigInteger n) //{ // PrimesBigInteger i = PrimesBigInteger.One; // while (i.CompareTo(rounds) <= 0) // { // if (Witness(PrimesBigInteger.RandomM(n.Subtract(PrimesBigInteger.Two)).Add(PrimesBigInteger.Two), n)) // return false; // i = i.Add(PrimesBigInteger.One); // } // return true; //} private void GetShift(PrimesBigInteger a) { m_d = m_Value - 1; m_shift = 0; while (m_d.Mod(2).CompareTo(0) == 0) { log.Info(string.Format(rsc.Primetest.mr_shiftright, m_d, m_d.ShiftRight(1))); m_d = m_d.ShiftRight(1); m_shift++; } }
private void btnForward_Click(object sender, RoutedEventArgs e) { PrimesBigInteger rows = PrimesBigInteger.ValueOf(this.Rows); PrimesBigInteger amount = rows; if (sender == btnCompleteForward) { amount = m_Limit.Subtract((numbergrid.Children[this.Rows + this.Columns - 2 + (this.Columns * this.Rows) - 1] as NumberButton).BINumber); amount = amount.Add(rows.Subtract(amount.Mod(rows))); } ScrollGrid(amount, false); }
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); }
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(); } }
protected override void DoExecute() { FireOnStart(); for (PrimesBigInteger from = m_From; from <= m_To; from = from + 1) { PrimesBigInteger counter = 0; for (PrimesBigInteger d = 1; d * 2 <= from; d = d + 1) { if (from.Mod(d).Equals(PrimesBigInteger.Zero)) { counter = counter + 1; FireOnMessage(this, from, counter.ToString()); } } counter = counter + 1; FireOnMessage(this, from, counter.ToString()); } FireOnStop(); }
protected override void DoExecute() { FireOnStart(); for (PrimesBigInteger from = m_From; from.CompareTo(m_To) <= 0; from = from + 1) { PrimesBigInteger sum = 0; for (PrimesBigInteger d = 1; d * 2 <= from; d = d + 1) { if (from.Mod(d).Equals(PrimesBigInteger.Zero)) { sum = sum + d; FireOnMessage(this, from, sum.ToString()); } } sum = sum + from; FireOnMessage(this, from, sum.ToString()); } FireOnStop(); }
private void DoCalculateGoldbach(object o) { if (o == null || o.GetType() != typeof(PrimesBigInteger)) { return; } PrimesBigInteger value = o as PrimesBigInteger; if (value.Mod(PrimesBigInteger.Two).Equals(PrimesBigInteger.One)) // value is odd { ControlHandler.SetPropertyValue(lblGoldbachInfoCalc, "Text", string.Format(Distribution.numberline_isodd, value)); ControlHandler.SetPropertyValue(gbGoldbach, "Visibility", Visibility.Collapsed); } else if (value.Equals(PrimesBigInteger.Two)) // value = 2 { ControlHandler.SetPropertyValue(lblGoldbachInfoCalc, "Text", Distribution.numberline_istwo); ControlHandler.SetPropertyValue(gbGoldbach, "Visibility", Visibility.Collapsed); } else // value is even and not prime { int counter = 0; int maxlines = 1000; if (!value.IsProbablePrime(10)) { long x = value.LongValue; int i = 0; long sum1 = PrimeNumbers.primes[i]; while (sum1 <= x / 2) { long sum2 = x - sum1; if (BigIntegerHelper.IsProbablePrime(sum2)) //if (PrimeNumbers.isprime.Contains(sum2)) { counter++; if (counter < maxlines) { logGoldbach.Info(string.Format("{0} + {1} ", sum1, sum2)); } else if (counter == maxlines) { logGoldbach.Info(string.Format(Distribution.numberline_goldbachmaxlines, maxlines)); } if (counter % 50 == 0) { string fmt = (counter == 1) ? Distribution.numberline_goldbachfoundsum : Distribution.numberline_goldbachfoundsums; ControlHandler.SetPropertyValue(lblGoldbachInfoCalc, "Text", string.Format(fmt, counter, value)); } } sum1 = (++i < PrimeNumbers.primes.Length) ? PrimeNumbers.primes[i] : (long)BigIntegerHelper.NextProbablePrime(sum1 + 1); } string fmt1 = (counter == 1) ? Distribution.numberline_goldbachfoundsum : Distribution.numberline_goldbachfoundsums; ControlHandler.SetPropertyValue(lblGoldbachInfoCalc, "Text", string.Format(fmt1, counter, value)); ControlHandler.SetPropertyValue(gbGoldbach, "Visibility", goldbachIsOpen ? Visibility.Visible : Visibility.Collapsed); } } if (GoldbachDone != null) { GoldbachDone(); } }
private void DoExecuteGraphic() { FireStartEvent(); ClearArrows(); m_SourceDestination.Clear(); log.Clear(); lock (m_RunningLockObject) { m_Running = true; } Point lastPoint = new Point(-1, -1); Ellipse lastEllipse = null; PrimesBigInteger result = null; PrimesBigInteger tmp = m_Base.Mod(m_Mod); Ellipse e = this.GetEllipseAt(m_Points[tmp.IntValue]); if (e != null) { ControlHandler.SetPropertyValue(e, "Fill", Brushes.Red); ControlHandler.SetPropertyValue(e, "Stroke", Brushes.Red); } PrimesBigInteger i = 1; while (i <= m_Exp) { Thread.Sleep(100); if (result == null) { result = m_Base.Mod(m_Mod); log.Info(string.Format(Primes.Resources.lang.Numbertheory.Numbertheory.powermod_executionfirst, 1, m_Base, m_Mod, result)); } else { PrimesBigInteger tmpResult = result; result = (result * m_Base).Mod(m_Mod); log.Info(string.Format(Primes.Resources.lang.Numbertheory.Numbertheory.powermod_execution, i, tmpResult, m_Base, m_Mod, result)); } if (lastPoint.X == -1 && lastPoint.Y == -1) { lastPoint = m_Points[result.IntValue]; lastEllipse = e; } else { Ellipse _e = this.GetEllipseAt(m_Points[result.IntValue]); Point newPoint = m_Points[result.IntValue]; //CreateArrow(i.Subtract(PrimesBigInteger.One), lastPoint, newPoint); CreateArrow(i - 1, lastEllipse, _e); lastPoint = newPoint; lastEllipse = _e; } i = i + 1; if (i >= 3) { WaitStepWise(); } } lock (m_RunningLockObject) { m_Running = false; } FireStopEvent(); }