简体   繁体   English

我的程序是否花费了太多时间来执行?

[英]Is my program taking too much time to execute?

I wanted to solve a question from project euleur about finding the largest prime number of a big number.我想解决欧拉项目中关于寻找大数中最大质数的问题。 I run my code on a virtual machine on Visual studio 2012, and the code seems froze.我在 Visual Studio 2012 的虚拟机上运行我的代码,代码似乎冻结了。 When I step into the loop, the code works well, but when I execute it, the console is always there.当我进入循环时,代码运行良好,但是当我执行它时,控制台总是在那里。 It is as if the program is still running.就好像程序还在运行一样。 Could it be that the program takes time to execute?可能是程序需要时间来执行?

My Code我的代码

static void Main(string[] args)
{
    long number = 5;

    for (long i = 1; i < 600851475143; i++)
    {
        if (i % 2 != 0 && i % 1 == 0 && i % i == 0)
            number = i;
    }

}

I ran this bit of code and it does take a while to run, but it does seem to progress (i does increment).我运行了这段代码,它确实需要一段时间才能运行,但它似乎确实在进步(我确实在增加)。 Try this to determine whether i is a prime:试试这个来确定 i 是否是素数:

    public static bool IsPrime(long candidate)
    {
        // Test whether the parameter is a prime number.
        if ((candidate & 1) == 0)
        {
            return candidate == 2;
        }
        // Note:
        // ... This version was changed to test the square.
        // ... Original version tested against the square root.
        // ... Also we exclude 1 at the very end.
        for (int i = 3; (i * i) <= candidate; i += 2)
        {
            if ((candidate % i) == 0)
            {
                return false;
            }
        }
        return candidate != 1;
    }

I can't claim credit for this.我不能为此声称功劳。 It is from http://www.dotnetperls.com/prime .它来自http://www.dotnetperls.com/prime

Add some Console.WriteLines to your main method to its progress:将一些 Console.WriteLines 添加到您的主要方法以了解其进度:

    static void Main(string[] args)
    {
        long number = 5;

        for (long i = 1; i < 600851475143; i++)
        {
            if (IsPrime(i))
            {
                Console.WriteLine(i);
                number = i;
            }
        }
    }

There's other resources out there for these algorithms too: http://csharpinoneroom.blogspot.com/2008/03/find-prime-numer-at-fastest-speed.html这些算法还有其他资源: http : //csharpinoneroom.blogspot.com/2008/03/find-prime-numer-at-fastest-speed.html

Good luck!祝你好运!

Your algorithm is incorrect.你的算法不正确。 Here is a simple way to find the prime factors of a composite number, suitable for Project Euler:这是一个简单的方法来找到一个合数的质因数,适用于 Project Euler:

function factors(n)
    f := 2
    while f * f <= n
        if n % f == 0
            output f
            n := n / f
        else
            f := f + 1
    output n

This works by dividing n by each f in succession, reducing n and outputting f whenever a prime factor is found.这是通过将n除以每个f来工作,减少n并在找到质因子时输出f The final factor is the remaining n when f is greater than the square root of n , at which point n must be prime.最后一个因素是剩余的n,f是大于n的平方根时,在该点n必须是素数。

There are other ways to factor integers.还有其他方法可以分解整数。 When you are ready for more, I modestly recommend this essay at my blog.当你准备好更多时,我谦虚地在我的博客上推荐这篇文章

Ultimately, it doesn't matter if you write the fastest code in the world if it doesn't work correctly.最终,如果它不能正常工作,你是否编写世界上最快的代码并不重要。 Yours doesn't, speed aside.你的没有,除了速度。

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

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