private static void LogPrime(BigInteger prime, TimeSpan timeElapsed, int cursorTopPosition, int count) { string primeText = prime.ToString(); if (Settings.Verbose_Mode) { string log10prime = (Math.Round(BigInteger.Log10(prime))).ToString("F0"); string log2prime = (Math.Round(CryptographicPrimeGenerator.Log2(prime))).ToString("F0"); Console.WriteLine("] {0} (time elapsed)", AggregateTimer.FormatTimeSpan(timeElapsed)); Console.WriteLine(); Console.WriteLine(" OUTPUT: ..\\{0}", Settings.LogFile_Primes.ToUpperInvariant()); int saveCursorTop = Console.CursorTop; Console.SetCursorPosition(0, cursorTopPosition); Console.WriteLine(" ACTUAL SIZE: {0} bits ({1} decimal digits)", log2prime, log10prime); Console.SetCursorPosition(0, saveCursorTop); Console.WriteLine(); string fileOutput = string.Format("{1} bit prime ({2} digits):{0}{3}{0}", Environment.NewLine, log2prime, log10prime, primeText); WriteLogFile(Settings.LogFile_Primes, fileOutput); } else { WriteLogFile(Settings.LogFile_Primes, primeText); } Console.Write(string.Format("({0})", count.ToString())); }
private void FindPrimes_WorkerComplete(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { WriteMessageBox(e.Error.ToString(), "Exception Message"); } else if (e.Cancelled) { WriteOutputTextboxLine("Task was canceled.", true); } else { BigInteger prime1 = (BigInteger)e.Result; string primeNumber = prime1.ToString(); string base10size = (Math.Round(BigInteger.Log10(prime1))).ToString("F0"); string base2size = (Math.Round(CryptographicPrimeGenerator.Log2(prime1))).ToString("F0"); //.... Print bit array DateTime bitStringStart1 = DateTime.Now; byte[] byteArray1 = prime1.ToByteArray(); BitArray bitArray1 = new BitArray(byteArray1); string bitString1 = new string( bitArray1.Cast <bool>() .Reverse() .SkipWhile(b => !b) .Select(b => b ? '1' : '0') .ToArray() ); //string bitCountString1 = bitString1.Length.ToString(); TimeSpan bitStringTime1 = DateTime.Now.Subtract(bitStringStart1); WriteOutputTextboxLine(""); //.... PRINT WriteOutputTextboxLine(string.Format("{0} bit prime ({1} digits):", base2size, base10size)); WriteOutputTextboxLine(primeNumber); //WriteOutputLine(string.Format("Bits ({0}):", log2prime)); //WriteOutputLine(bitString1+Environment.NewLine); //... Print run/processing time if (algorithmWorker.RuntimeTimer != null && algorithmWorker.RuntimeTimer != TimeSpan.Zero) { WriteOutputTextboxLine(string.Format("Run time: {0}", AggregateTimer.FormatTimeSpan(algorithmWorker.RuntimeTimer))); WriteOutputTextboxLine(string.Format("Time to render: {0}", AggregateTimer.FormatTimeSpan(bitStringTime1))); } } DisposeThreadedAlgorithmWorker(); IsBusy = false; SetButtonText(true, "Search for primes..."); }
internal static void Main() { //AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; int counter = 1; int sizeTextPosition = 0; int max = Settings.Quantity; int primeBitSize = Settings.Prime_BitSize; Log.LogFilename = Settings.LogFile_Methods; Console.CursorVisible = false; ConsoleWorker worker = new ConsoleWorker(Settings.Logging_Enabled); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Maurer Prime Finding Algorithm (Console)"); Console.WriteLine(DashedLine); Console.WriteLine("TO EXIT: [ESC] or [Q]"); Console.WriteLine(); while (counter <= max) { if (Settings.Verbose_Mode) { Console.WriteLine(DashedLine); Console.WriteLine("({0} of {1})", counter, max); Console.WriteLine(); Console.WriteLine(" TARGET SIZE: {0} bits", primeBitSize); sizeTextPosition = Console.CursorTop; Console.WriteLine(); Console.WriteLine(); Console.Write(" ["); } if (!worker.StartWorker(primeBitSize)) { break; } while (worker.IsBusy) { Thread.Sleep(Settings.ThreadSleep_Duration); // Sleep a bit if (Console.KeyAvailable) { ConsoleKeyInfo cki = Console.ReadKey(true); if (cki.Key == ConsoleKey.Escape || cki.Key == ConsoleKey.Q) { worker.CancelWorker(); Console.WriteLine(); Console.WriteLine("Abort key received."); Console.WriteLine(); break; } } } if (worker.Result == WorkerResultType.Canceled) { break; } else if (worker.Result == WorkerResultType.None) { Console.WriteLine(); Console.WriteLine("Result object empty! Aborting..."); break; } else if (worker.Result == WorkerResultType.Error) { Exception error = worker.RemoveErrorResult(); DisplayError(error); break; } else if (worker.Result == WorkerResultType.Success) { BigInteger prime = worker.RemoveSuccessResult(); LogPrime(prime, worker.RunTime, sizeTextPosition, counter); } counter++; } if (!string.IsNullOrWhiteSpace(Settings.LogFile_Timers)) { WriteLogFile(Settings.LogFile_Timers, "Prime bit size: " + primeBitSize); WriteLogFile(Settings.LogFile_Timers, "MillerRabin.OutsideExecutionTime: " + AggregateTimer.FormatTimeSpan(MillerRabin.OutsideExecutionTime)); WriteLogFile(Settings.LogFile_Timers, "MillerRabin.InsideExecutionTime: " + AggregateTimer.FormatTimeSpan(MillerRabin.InsideExecutionTime)); WriteLogFile(Settings.LogFile_Timers, "Log.TotalExecutionTime: " + AggregateTimer.FormatTimeSpan(Log.TotalExecutionTime)); WriteLogFile(Settings.LogFile_Timers, "Eratosthenes.TotalExecutionTime: " + AggregateTimer.FormatTimeSpan(Eratosthenes.TotalExecutionTime)); WriteLogFile(Settings.LogFile_Timers, "TrialDivision.TotalExecutionTime: " + AggregateTimer.FormatTimeSpan(TrialDivision.TotalExecutionTime)); WriteLogFile(Settings.LogFile_Timers, "CryptoRandomSingleton.TotalExecutionTime: " + AggregateTimer.FormatTimeSpan(CryptoRandomSingleton.TotalExecutionTime)); WriteLogFile(Settings.LogFile_Timers, Environment.NewLine); } if (worker.Result == WorkerResultType.Canceled) { Log.Message("*** OPERATION ABORTED ***"); Console.WriteLine(DashedLine); Console.WriteLine(); Console.WriteLine("Aborted. Hit any key to terminate..."); Console.ReadLine(); return; } while (worker.Result != WorkerResultType.None) { if (worker.Result == WorkerResultType.Error) { Exception error = worker.RemoveErrorResult(); DisplayError(error); } else if (worker.Result == WorkerResultType.Success) { BigInteger prime = worker.RemoveSuccessResult(); LogPrime(prime, worker.RunTime, sizeTextPosition, counter); } } Console.WriteLine(DashedLine); Console.WriteLine(); Console.WriteLine("Finished. Hit any key to terminate..."); Console.ReadLine(); Console.ResetColor(); }