我试图在C#中得到前1000个素数的总和,但我使用的代码非常慢,需要永远计算,到目前为止还没有返回有效总和.
我是新手,我希望你们中的任何人都能看看,帮助我提高代码效率,让我知道自己做错了什么.如果我在论坛规则方面做错了,也请告诉我.
提前感谢您宝贵的时间!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
long sumOfPrime=0;
Console.WriteLine("Calculating Sum of Prime");
for (int i = 1, primeCounter = 0; primeCounter <= 1000; ++i)
{
if (!IsPrime(i))
{
continue;
}
else
{
primeCounter = +1;
sumOfPrime = +i;
}
}
Console.WriteLine(sumOfPrime);
}
static bool IsPrime(int number)
{
if (number == 1)
{
return false;
}
if (number == 2)
{
return true; …Run Code Online (Sandbox Code Playgroud) c# ×1