protected override void DoExecute() { FireOnStart(); ControlHandler.SetPropertyValue(m_tbCalcInfo, "Visibility", Visibility.Visible); StringBuilder sb = new StringBuilder(); var factors = (m_Factors != null) ? m_Factors : m_Value.Factorize(); Dictionary <PrimesBigInteger, long> f = new Dictionary <PrimesBigInteger, long>(); List <PrimesBigInteger> keys = factors.Keys.ToList(); foreach (var key in keys) { f[key] = 0; } Dictionary <PrimesBigInteger, PrimesBigInteger> result = new Dictionary <PrimesBigInteger, PrimesBigInteger>(); PrimesBigInteger sum = PrimesBigInteger.Zero; int i; do { PrimesBigInteger phi = PrimesBigInteger.Phi(f); result[PrimesBigInteger.Refactor(f)] = phi; sum = sum.Add(phi); for (i = keys.Count - 1; i >= 0; i--) { f[keys[i]]++; if (f[keys[i]] <= factors[keys[i]]) { break; } } for (int j = i + 1; j < keys.Count; j++) { f[keys[j]] = 0; } }while (i >= 0); SetCalcInfo(string.Format(Primes.Resources.lang.WpfControls.Distribution.Distribution.numberline_eulerphisuminfo, m_Value, sum)); List <PrimesBigInteger> philist = result.Keys.Select(k => k).ToList(); philist.Sort(PrimesBigInteger.Compare); foreach (var k in philist) { m_Log.Info(string.Format(Primes.Resources.lang.WpfControls.Distribution.Distribution.numberline_eulerphisumlog, k, result[k])); } String s = String.Join(" + ", philist.Select(k => String.Format("φ({0})", k))); m_Log.Info(s + " = " + sum); FireOnStop(); }
protected override void DoExecute() { FireOnStart(); ControlHandler.SetPropertyValue(m_tbCalcInfo, "Visibility", Visibility.Visible); if (m_Value.IsPrime(20)) { string info = string.Format(Primes.Resources.lang.WpfControls.Distribution.Distribution.numberline_eulerphiisprime, m_Value, m_Value, m_Value, m_Value.Subtract(PrimesBigInteger.One)); m_Log.Info(info); SetCalcInfo(info); } else { PrimesBigInteger phi = (m_Factors != null) ? PrimesBigInteger.Phi(m_Factors) : m_Value.Phi(); SetCalcInfo(string.Format(Primes.Resources.lang.WpfControls.Distribution.Distribution.numberline_eulerphifoundresult, phi, m_Value)); BigInteger x = BigInteger.Parse(m_Value.ToString()); int counter = 0; int maxlines = 1000; for (BigInteger d = 1; d < x; d++) { if (BigInteger.GreatestCommonDivisor(d, x) == 1) { m_Log.Info(d + " "); if (++counter >= maxlines) { break; } } } if (counter >= maxlines) { m_Log.Info(string.Format(Primes.Resources.lang.WpfControls.Distribution.Distribution.numberline_eulerphimaxlines, maxlines, phi)); } } FireOnStop(); }
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) { } }