Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中前1000个素数的和#_C# - Fatal编程技术网

C# C中前1000个素数的和#

C# C中前1000个素数的和#,c#,C#,我试图得到C#中前1000个素数的和,但是我使用的代码非常慢,需要花费很长时间来计算,到目前为止还没有返回有效的和 我是新手,我希望你们中的任何人都能看看,帮助我提高代码的效率,并让我知道我做错了什么。如果我在论坛规则方面有任何错误,也请告诉我 提前感谢您宝贵的时间 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

我试图得到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; 
            }

            for (int i = 2; i <= Math.Ceiling(Math.Sqrt(number)); ++i)
            { 
                if (number % i == 0) 
                { 
                    return false; 
                } 
            }

            return true;
        }
    }
}       
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序3
{
班级计划
{
静态void Main(字符串[]参数)
{
长sumOfPrime=0;
控制台写入线(“计算素数之和”);
对于(inti=1,primeCounter=0;primeCounter您的错误在于:

primeCounter = +1
每次都会重置计数器。我想你的意思是

primeCounter += 1
…这会增加它。甚至更好:

primeCounter++

以下返回3682913作为前1000个素数的总和,并且在不到一秒钟的时间内返回。我不确定我是否遵循了iPrime方法for循环中的逻辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Find1000Primes
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalPrime = 0;
            int countPrime = 1000;
            int x = 0;  // counter for the primes
            int i = 0;  // each number that is checked starting with 0

            while (x < countPrime)
            {
                bool prime = IsPrime(i);
                if (prime)
                {
                    totalPrime = totalPrime + i;
                    x++;
                }
                i++;
            }
            Console.WriteLine(totalPrime.ToString());
            Console.ReadLine();
        }

        public static bool IsPrime(int number)
        {
            if ((number & 1) == 0)
            {
                if (number == 2)
                    return true;
                else
                    return false;
            }

            for (int i = 3; (i*i) <= number; i+=2)
            {
                if ((number % i) == 0)
                    return false;
            }
            return number != 1;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间查找1000个素数
{
班级计划
{
静态void Main(字符串[]参数)
{
整数totalPrime=0;
int countPrime=1000;
int x=0;//素数的计数器
int i=0;//从0开始检查的每个数字
while(x对于(int i=3;(i*i)那么,首先试着找到前1000个素数。在这样做的时候,你可以输出结果,这样你就可以找到a)如果得到的数字在第一位有效,b)慢下来的地方。一旦你在一个数组或列表中有了1000个数字,对它们求和应该是微不足道的。这个问题属于另一个网站,比如看一下生成素数的各种算法。有更快的算法。使用:
+=
而不是:
=+
你可以使用类似的方法。它应该是f是吗?OP的
IsPrime
方法的逻辑与您的方法相同,只是您将偶数视为特例,而OP会在
for
循环中检查偶数以及其他可能的除数。比只检查奇数更好的是只检查p的素数在Linux上使用C++的方法,我几乎能即时得到结果。