项目欧拉10号#

idi*_*dik 1 c#

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
Run Code Online (Sandbox Code Playgroud)

我的回答是:

bool IsRishoni;
int soap = 0;
for (int i = 3; i < 2000000; i++)
{
    IsRishoni = true;
    for (int a = 2; (a <= Math.Sqrt(i)) && (IsRishoni); a++)
    {
        if (i % a == 0)
            IsRishoni = false;
    }
    if (IsRishoni)
    {
        soap = i + soap;
    }
}
Console.WriteLine(soap + 2);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?我得到的答案是1179908154 ...请帮忙.

And*_*ahl 6

更换

soap = i + soap;
Run Code Online (Sandbox Code Playgroud)

soap = checked(i + soap);
Run Code Online (Sandbox Code Playgroud)

..问题应该暴露出来.

这个问题有更多细节:C#中的int没有溢出异常?

  • 嗯,这个问题在你被给予链接的那个问题中描述.你看过吗? (2认同)