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);
        }
Ejemplo n.º 6
0
        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}");
            }
        }