Exemple #1
0
        public static bool IsPrime(int n, PrimeTestType testType)
        {
            switch (testType)
            {
            case PrimeTestType.TrialDivision:
                return(IsTrialPrime(n));

            case PrimeTestType.EratosthenesSieve:
                return(IsEratosthenesPrime(n));

            default:
                throw new ArgumentOutOfRangeException(nameof(testType), testType, "Unknown test specified!");
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            while (true)
            {
                var menuChoice = ReadVariantFromConsole(MenuVariants);

                if (0 == menuChoice)
                {
                    return;
                }

                PrimeTestType.TryParse(MenuVariants[menuChoice], out PrimeTestType testType);

                uint n;
                do
                {
                    Console.Write("N = ");

                    var isValid = uint.TryParse(Console.ReadLine(), out n);

                    if (isValid && n < MaxNumber)
                    {
                        break;
                    }

                    if (!isValid)
                    {
                        Console.WriteLine("Invalid number");
                    }

                    if (n >= MaxNumber)
                    {
                        Console.WriteLine("Number is too big");
                    }
                } while (true);


                var isNPrime = IsPrime((int)n, testType);


                Console.WriteLine("{0} is {1}prime", n, isNPrime ? "" : "not ");
            }
        }