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); }
/// <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) { 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(); }
public void MarkNumber(PrimesBigInteger number, Brush color) { try { if (!m_MarkedNumbers.ContainsKey(number)) { m_MarkedNumbers.Add(number, color); } else { m_MarkedNumbers[number] = color; } UIElementCollection buttons = ControlHandler.GetPropertyValue(numbergrid, "Children") as UIElementCollection; int i = (int)ControlHandler.GetPropertyValue(buttons, "Count"); NumberButton first = (buttons[Columns + Rows - 2] as NumberButton); if (first.BINumber.Add(number).CompareTo(PrimesBigInteger.ValueOf(Columns * Rows)) < 0) { int index = number.Subtract(first.BINumber).IntValue + Columns + Rows - 2; NumberButton btn = buttons[index] as NumberButton; ControlHandler.SetPropertyValue(btn, "Background", color); } } catch (Exception e) { } }
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); }
void iscTo_Execute(PrimesBigInteger value) { if (value.CompareTo(PrimesBigInteger.Two.Add(ButtonScaleMinusOne)) >= 0) { iscFrom.ResetMessages(); iscTo.ResetMessages(); EnableInput(); PrimesBigInteger diff = value.Subtract(m_Start.Add(ButtonScaleMinusOne)); DoAtomicScroll(diff); MarkNumberWithOutThreads(value); } }
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 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 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; }
private void DoAtomicScroll(PrimesBigInteger amount) { PrimesBigInteger len = PrimesBigInteger.ValueOf(m_Buttons.Count - 1); PrimesBigInteger newStart = m_Start.Add(amount); if (newStart.Add(len).CompareTo(MAX) > 0) { newStart = MAX.Subtract(len); } else if (newStart.CompareTo(MIN) < 0) { newStart = MIN; } amount = newStart.Subtract(m_Start); m_Start = newStart; m_End = m_Start.Add(len); m_ButtonsDict.Clear(); foreach (NumberButton btn in m_Buttons) { PrimesBigInteger number = (ControlHandler.GetPropertyValue(btn, "BINumber") as PrimesBigInteger).Add(amount); ControlHandler.SetPropertyValue(btn, "BINumber", number); m_ButtonsDict.Add(number, btn); SetButtonColor(btn); } if (m_ActualNumber.CompareTo(m_Start) < 0) { m_ActualNumber = m_Start; } if (m_ActualNumber.CompareTo(m_End) > 0) { m_ActualNumber = m_End; } MarkNumberWithOutThreads(m_ActualNumber); SetFromTo(); SetCountPrimes(); }
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)); }
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) { } }
public bool GetValue(ref PrimesBigInteger from, ref PrimesBigInteger to) { from = null; to = null; ResetMessages(); if (m_RbSelection == Selection.Free) { ValidateFreeInput(ref from, ref to); } else if (m_RbSelection == Selection.Calc) { ValidateCalcInput(ref from, ref to); } if (from != null && to != null) { if (this.m_ValueValidators.ContainsKey(From)) { IValidator <PrimesBigInteger> validator = this.m_ValueValidators[From]; validator.Value = from; if (validator.Validate(ref from) != Primes.WpfControls.Validation.ValidationResult.OK) { if (m_RbSelection == Selection.Free) { InfoFree(validator.Message, new TextBox[] { m_tbFromFree }, validator.HelpLink); } else if (m_RbSelection == Selection.Calc) { InfoFree(validator.Message, new TextBox[] { m_tbFromCalcFactor, m_tbFromCalcBase, m_tbFromCalcExp, m_tbFromCalcSum }, validator.HelpLink); } from = null; to = null; } } if (this.m_ValueValidators.ContainsKey(To)) { IValidator <PrimesBigInteger> validator = this.m_ValueValidators[To]; validator.Value = to; if (validator.Validate(ref to) != Primes.WpfControls.Validation.ValidationResult.OK) { if (m_RbSelection == Selection.Free) { InfoFree(validator.Message, new TextBox[] { m_tbToFree }, validator.HelpLink); } else if (m_RbSelection == Selection.Calc) { InfoCalc(validator.Message, new TextBox[] { m_tbToCalcFactor, m_tbToCalcBase, m_tbToCalcExp, m_tbToCalcSum }, validator.HelpLink); } from = null; to = null; } } if (m_RangeValueValidator != null && from != null && to != null) { m_RangeValueValidator.Value = to.Subtract(from); PrimesBigInteger range = null; if (m_RangeValueValidator.Validate(ref range) != Primes.WpfControls.Validation.ValidationResult.OK) { if (m_RbSelection == Selection.Free) { InfoFree(m_RangeValueValidator.Message, new TextBox[] { m_tbFromFree, m_tbToFree }, m_RangeValueValidator.HelpLink); } else if (m_RbSelection == Selection.Calc) { InfoCalc(m_RangeValueValidator.Message, new TextBox[] { m_tbFromCalcFactor, m_tbFromCalcBase, m_tbFromCalcExp, m_tbFromCalcSum, m_tbToCalcFactor, m_tbToCalcBase, m_tbToCalcExp, m_tbToCalcSum }, m_RangeValueValidator.HelpLink); } from = null; to = null; } } foreach (IValidator <PrimesBigInteger> validator in this.m_SingleAdvisors[From]) { if (validator.Validate(ref from) != Primes.WpfControls.Validation.ValidationResult.OK) { if (m_RbSelection == Selection.Free) { InfoFree(validator.Message, new TextBox[] { m_tbFromFree }, validator.HelpLink); } else if (m_RbSelection == Selection.Calc) { InfoCalc(validator.Message, new TextBox[] { m_tbFromCalcFactor, m_tbFromCalcBase, m_tbFromCalcExp, m_tbFromCalcSum }, validator.HelpLink); } break; } } foreach (IValidator <PrimesBigInteger> validator in this.m_SingleAdvisors[To]) { if (validator.Validate(ref to) != Primes.WpfControls.Validation.ValidationResult.OK) { if (m_RbSelection == Selection.Free) { InfoFree(validator.Message, new TextBox[] { m_tbToFree }, validator.HelpLink); } else if (m_RbSelection == Selection.Calc) { InfoCalc(validator.Message, new TextBox[] { m_tbToCalcFactor, m_tbToCalcBase, m_tbToCalcExp, m_tbToCalcSum }, validator.HelpLink); } break; } } } bool result = from != null && to != null; return(result); }