简体   繁体   English

为什么我找到最大素数的程序永远不会写入控制台?

[英]Why does my program for finding the largest prime never writes to console?

I debugged my code and everything works perfectly. 我调试了我的代码,一切都很完美。 But my code never writes to console for some reason. 但是我的代码由于某种原因从未写入控制台。

Here is my code: 这是我的代码:

  long largest = 0;


        for (long i = 1; i < 600851475144; i++)
        {
            long check = 0;
            for (long j = 1; j < i + 1; j++)
            {

                if ((i%j) == 0)
                {
                    check++;
                }
            }
            if (check == 2)
            {
                largest = i;
            }
        }

        Console.WriteLine(largest);
        Console.ReadKey();

Question: How do I write to console? 问题:如何写入控制台?

What? 什么?

It will finish, but it will last forever because of all the iterations it has to do. 它将完成,但它将永远持续,因为它必须做的所有迭代。

Prime finding calculation is a very intensive calculation, specially in the way you have done it. 寻找计算是一项非常密集的计算,特别是以您的方式完成。

Summarizing, it does not return because you have to wait minutes/hours/days/years? 总结一下,它不会返回,因为你必须等待几分钟/小时/天/年? To compute that. 要计算它。

Your algorithm is too slow to complete in a reasonable time, so you need to come up with an alternative approach. 您的算法太慢而无法在合理的时间内完成,因此您需要提出另一种方法。

First, the algorithm has to stop checking the naive definition (two divisors). 首先,算法必须停止检查天真的定义(两个除数)。 If you check all divisors up to square root of the number, and did not find any, the number is prime. 如果你检查所有除数直到数字的平方根,并且没有找到任何除数,则该数字为素数。 Second, if you are looking for the largest prime in a range, start at the top of the range, go down, and stop as soon as you find the first prime. 其次,如果您正在寻找范围内的最大素数,请从该范围的顶部开始,向下,并在找到第一个素数时立即停止。 Third, there is no point to try even numbers. 第三,没有必要尝试偶数。

Implementing these three changes will get your algorithm running in time. 实现这三个更改将使您的算法及时运行。

Your algorithm is poor. 你的算法很差。 It has to make a lot of iterations. 它必须进行大量的迭代。 As others have already mentioned, there is no sense to divide by even numbers thus incremen by two, start with 3, you can reduce the iteration count to the square root of given number. 正如其他人已经提到的那样,没有意义除以偶数,因此增加2,从3开始,你可以将迭代计数减少到给定数字的平方根。 mine is not perfect as well but it finishes in a blink. 我的也不完美,但它眨眼就结束了。 The idea is to reduce count of iterations by dividing the given number by all found divisors. 我们的想法是通过将给定数除以所有找到的除数来减少迭代次数。 Try at your own risk! 请自担风险!

    long FindLargestPrimeDivisor(long number)
    {
        long largestPrimeDivisor = 1;
        while (true)
        {
            if (number % largestPrimeDivisor == 0)
            {
                number /= largestPrimeDivisor;
            }
            if (number < largestPrimeDivisor)
            {
                break;
            }
            largestPrimeDivisor++;
        }
        return largestPrimeDivisor;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM