有人可以解释为什么一个数字越来越高将计算,但我的实际数字不会?

时间:2018-03-11 03:03:50

标签: c# stopwatch ulong

所以,这个控制台应用程序只是打印一个数字是否为素数,几乎每个数字都适用,但是当要求分配时间测试两个硬编码的数字(7389274937501454911和9389274937501454911)时,这些数字将无法打印输出任何内容但是两个数字的+1或-1将打印出来。

using System;

namespace IsPrime
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string isPlaying = "y";
            bool isPrime;

            while(isPlaying == "y")
            {
                var timer = System.Diagnostics.Stopwatch.StartNew ();
                isPrime = isPrimeNum(9389274937501454911);
                if (isPrime)
                {
                    Console.WriteLine(9389274937501454911 + " is Prime");
                }
                else 
                    Console.WriteLine(9389274937501454911 + " is not prime");

                isPrime = isPrimeNum(7389274937501454911);
                if (isPrime)
                {
                    Console.WriteLine(7389274937501454911 + " is Prime");
                }
                else 
                    Console.WriteLine(7389274937501454911 + " is not prime");
                timer.Stop ();
                var time = timer.ElapsedMilliseconds;
                Console.WriteLine("Elasped Time: " + time + "\n");

                Console.WriteLine("Do you want to play again?: y / n");
                isPlaying = Console.ReadLine ();

            }
        }

        static bool isPrimeNum(ulong n)
        {
            if (n == 1 || n == 2)
                return true;

            if (n % 2 == 0)
                return false;

            for(ulong i = 3; i < n; i = i + 2) 
            {
                if (n % i == 0)
                    return false;
            }

            return true;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我已经改编了你的代码,现在就试试

using System;

namespace IsPrime
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string isPlaying = "y";
            bool isPrime;

            while(isPlaying == "y")
            {
                var timer = System.Diagnostics.Stopwatch.StartNew ();
                isPrime = IsPrime(9389274937501454911);
                if (isPrime)
                {
                    Console.WriteLine(9389274937501454911 + " is Prime");
                }
                else 
                    Console.WriteLine(9389274937501454911 + " is not prime");

                isPrime = IsPrime(7389274937501454911);
                if (isPrime)
                {
                    Console.WriteLine(7389274937501454911 + " is Prime");
                }
                else 
                    Console.WriteLine(7389274937501454911 + " is not prime");
                timer.Stop ();
                var time = timer.ElapsedMilliseconds;
                Console.WriteLine("Elasped Time: " + time + "\n");

                Console.WriteLine("Do you want to play again?: y / n");
                isPlaying = Console.ReadLine ();

            }
        }

        public static bool IsPrime(ulong number)
        {
            if (number == 1) return false;
            if (number == 2) return true;
            if (number % 2 == 0) return false;

           //This returns the whole number greater than or equal to the specified number, and in this case
           //In this case, I take the square root, obvious to divide in half, so the variable i increases twice, since I'm working with the biggest number and divided in half
            var boundary = (ulong)Math.Floor(Math.Sqrt(number));

            for (ulong i = 3; i <= boundary; i += 2)
            {
                if (number % i == 0) return false;
            }

            return true;
        }
    }
}