Пример #1
0
        public static Task <int> PrimeCheckTask(int from, int to)
        {
            var cnt = 0;

            return(Task.Run(() =>
            {
                for (int i = from; i < to; i++)
                {
                    try
                    {
                        if (PrimeCheck.IsPrimeNumber(i))
                        {
                            cnt++;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                }
                return cnt;
            }));
        }
Пример #2
0
        static async Task Main(string[] args)
        {
            Console.Title = "Prime number calculator";
            do
            {
                string input  = null;
                var    number = -1;
                do
                {
                    do
                    {
                        Console.WriteLine("-----------------------------------------------------");
                        Console.WriteLine("Type end for closing the application.");
                        Console.WriteLine("Type clear for clean console.");
                        Console.WriteLine("Type check for checking a specific number.");
                        Console.WriteLine("Or, enter an upper limit (integer):");
                        Console.WriteLine("Or, type kgv for evaluating the kgv of two integers:");
                        Console.WriteLine("-----------------------------------------------------");


                        input = Console.ReadLine();
                        if (input == "end")
                        {
                            return;
                        }
                    } while (string.IsNullOrEmpty(input));

                    if (input == "clear")
                    {
                        Console.Clear();
                    }


                    if (input == "kgv")
                    {
                        Console.WriteLine("-----------------------------------------------------");
                        Console.WriteLine("Ok, lets get the KGV, enter the first value:");
                        Console.WriteLine("-----------------------------------------------------");
                        var small = Console.ReadLine();
                        Console.WriteLine("Ok, now the second value:");
                        var big = Console.ReadLine();

                        int smallNUM = int.Parse(small);
                        int bigNUM   = int.Parse(big);
                        var kgv      = SimpleMathOps.KGV_multiple(smallNUM, bigNUM);
                        Console.WriteLine($"The kgv of {smallNUM} and {bigNUM} is {kgv}");
                        input = "continue";
                    }

                    if (input == "check")
                    {
                        Console.WriteLine("-----------------------------------------------------");
                        Console.WriteLine("Ok, lets check your number:");
                        Console.WriteLine("-----------------------------------------------------");
                        input = Console.ReadLine();

                        try
                        {
                            number = int.Parse(input);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }

                        var isPrime = false;
                        try
                        {
                            isPrime = PrimeCheck.IsPrimeNumber(number);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }

                        Console.WriteLine($"Number {number} is prime? {isPrime}");
                        input  = "continue";
                        number = -1;
                    }

                    if (input == "continue")
                    {
                        continue;
                    }
                    try
                    {
                        number = int.Parse(input);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                } while (number == -1);

                Console.WriteLine("Result:");
                await PrimeCheck.GetAllPrimeNumbersAsync(number);
            } while (true);
        }