このコードを実行しましたが、実行に時間がかかりますが、進行しているように見えます (インクリメントします)。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;
}
私はこれについて信用を主張することはできません。http://www.dotnetperls.com/primeからです。
いくつかの 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;
}
}
}
これらのアルゴリズムに関する他のリソースもあります:
http://csharpinoneroom.blogspot.com/2008/03/find-prime-numer-at-fastest-speed.html
幸運を!