public void VsaucePrime() { var actual = PrimeUtilties.GetFirstNPrimes(3336); ulong expected = 30941; Assert.Equal(expected, actual.Last()); }
public void HundredthPrimeNumberIs541() { var actual = PrimeUtilties.GetFirstNPrimes(100); ulong expected = 541; Assert.Equal(expected, actual.Last()); }
public void PF_Of_11() { var expected = new Dictionary <ulong, uint> { { 11, 1 } }; var actual = PrimeUtilties.GetPrimeFactorization(11); Assert.Equal(expected, actual); }
public void PF_Of_34643() { var expected = new Dictionary <ulong, uint> { { 7, 3 }, { 101, 1 } }; var actual = PrimeUtilties.GetPrimeFactorization(34643); Assert.Equal(expected, actual); }
public void PF_Of_999() { var expected = new Dictionary <ulong, uint> { { 3, 3 }, { 37, 1 } }; var actual = PrimeUtilties.GetPrimeFactorization(999); Assert.Equal(expected, actual); }
public void PF_Of_250() { var expected = new Dictionary <ulong, uint> { { 2, 1 }, { 5, 3 } }; var actual = PrimeUtilties.GetPrimeFactorization(250); Assert.Equal(expected, actual); }
public void PF_Of_7546() { var expected = new Dictionary <ulong, uint> { { 2, 1 }, { 7, 3 }, { 11, 1 } }; var actual = PrimeUtilties.GetPrimeFactorization(7546); Assert.Equal(expected, actual); }
private static void ShowPrimeFacts(ulong input) { try { var nfi = new CultureInfo("en-US", false).NumberFormat; nfi.NumberDecimalDigits = 0; var table = new Table() .AddColumn($"[bold green3]{input.ToString("N", nfi)}[/]") .AddColumn("Prime") .AddColumn("Mersenne"); var stopWatch = Stopwatch.StartNew(); bool isPrime = input.IsPrime(); bool isMersenne = input.IsMersenneNumber(); var isPrimeStr = isPrime ? ":check_mark_button:" : ":cross_mark:"; var isMersStr = isMersenne ? ":check_mark_button:" : ":cross_mark:"; //Console.WriteLine($"{input} {isPrimeStr} and {isMersStr}."); table.AddRow("", $"{isPrimeStr}", $"{isMersStr}"); AnsiConsole.Render(table); if (!isPrime) { foreach (var factor in PrimeUtilties.GetPrimeFactorization(input)) { var power = factor.Value > 1 ? $" ^ {factor.Value}" : string.Empty; Console.WriteLine($" > {factor.Key}{power}"); } } stopWatch.Stop(); if (stopWatch.ElapsedMilliseconds < 1000) { Console.WriteLine("Calculation took less than a second."); } else { Console.WriteLine($"Calculation took about {stopWatch.ElapsedMilliseconds / 1000} seconds."); } } catch (Exception e) { Console.WriteLine($"An error was encountered: {e.Message}"); } }
static void Main(string[] args) { if (args.Length < 2) { ShowUsage(); } else { var command = args[0]; var input = args[1]; if (!ulong.TryParse(input, out var result)) { Console.WriteLine("Fenumbler: Only positive 64-bit integers are valid for the second argument."); } else { switch (command) { case "-isprime": ShowPrimeFacts(result); break; case "-count": var i = 0; foreach (var prime in PrimeUtilties.GetFirstNPrimes(result)) { Console.WriteLine($"{++i}: {prime}"); } break; case "-mersenne": var j = 0; foreach (var mersenne in PrimeUtilties.GetFirstNMersennes(result)) { Console.WriteLine($"{++j}: {mersenne}"); } break; default: ShowUsage(); break; } } } }
public void OneIsNotAPrimeNumber() { var actual = PrimeUtilties.IsPrime(1); Assert.False(actual); }
public void GetNextPrimeReturnsCorrectValue(ulong input, ulong expected) { var actual = PrimeUtilties.GetNextPrime(input); Assert.Equal(expected, actual); }
public void FortyNineIsNotAPrimeNumber() { var actual = PrimeUtilties.IsPrime(49); Assert.False(actual); }
public void TwoIsAPrimeNumber() { var actual = PrimeUtilties.IsPrime(2); Assert.True(actual); }