private void PiTester_DoWork(object sender, DoWorkEventArgs e) { long length = (long)e.Argument; PiWorkResult result = new PiWorkResult(length); Console.WriteLine("Trying length " + length + "..."); for (long j = 0; j <= 2048 - length; j++) { string currentSubString = PI_2048.Substring((int)j, (int)length); if (currentSubString.EndsWith("2") || currentSubString.EndsWith("4") || currentSubString.EndsWith("6") || currentSubString.EndsWith("8") || currentSubString.EndsWith("0") || currentSubString.EndsWith("5")) { continue; } BigInteger currentNumber = BigInteger.Parse(currentSubString); if ((BigInteger.Remainder(BigInteger.Subtract(currentNumber, 1), 4) != 0) && (BigInteger.Remainder(BigInteger.Subtract(currentNumber, 3), 4) != 0)) { continue; } if ((BigInteger.Remainder(BigInteger.Subtract(currentNumber, 1), 6) != 0) && (BigInteger.Remainder(BigInteger.Add(currentNumber, 1), 6) != 0)) { continue; } if (primalityTester.IsPrimeMillerRabin(currentNumber)) { result.Results.Add(currentNumber.ToString()); } } e.Result = result; }
private void PiTester_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; PiWorkResult result = (PiWorkResult)e.Result; Console.WriteLine("Completed length " + result.Length + " finished."); foreach (string possiblePrimeString in result.Results) { Console.WriteLine(possiblePrimeString); } if (Interlocked.Read(ref this.currentLength) > 600) { worker.RunWorkerAsync(Interlocked.Decrement(ref this.currentLength)); } }