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 ...请帮忙.
更换
soap = i + soap;
Run Code Online (Sandbox Code Playgroud)
同
soap = checked(i + soap);
Run Code Online (Sandbox Code Playgroud)
..问题应该暴露出来.
这个问题有更多细节:C#中的int没有溢出异常?
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |