Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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#_Algorithm - Fatal编程技术网

C# 用平方根求素数

C# 用平方根求素数,c#,algorithm,C#,Algorithm,问题是 求200万以下所有素数之和 我的代码列出了很多非素数,比如9,15…,怎么了 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace p10 { // The sum of the primes below 10 is 2 + 3 +

问题是

求200万以下所有素数之和

我的代码列出了很多非素数,比如9,15…,怎么了

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

namespace p10
{
    // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
    class Program
    {
        static List<long> list = new List<long>();
        static void Main(string[] args)
        {
            Stopwatch sw = Stopwatch.StartNew();
            list.Add(2);
            list.Add(3);
            long x = list.Last();
            while (x < 2000000)
            {
                x += 2;
                FindPrime(x);
            }
            long y = list.Sum() - list.Last();
            Console.WriteLine(y);
            Console.WriteLine("Time used (float): {0} ms", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time used (rounded): {0} ms", sw.ElapsedMilliseconds);
            Console.ReadKey();
        }

        static void FindPrime(Int64 p)
        {
            Int64 max = (Int64)Math.Ceiling(Math.Sqrt(p));
            foreach (long n in list)
            {
                while (n <= max)
                {
                    if (p / n == 0)
                    {
                        continue;
                    }
                    else
                    {
                        list.Add(p);
                        break;
                    }
                }break;
            }
        }
    }
}

当你测试一个数是否可以被另一个数整除时,你想知道余数是否为零。更改此项:

if (p / n == 0)
为此:

if (p % n == 0)
但是看起来你的循环还是有问题。您可以将其改写为:

static void FindPrime(long p)
{
    long max = (long)Math.Ceiling(Math.Sqrt(p));
    foreach (long n in list)
    {
        if (n > max) 
        {
            break;
        }
        else if (p % n == 0)
        {
            return;
        }
    }

    list.Add(p);
}

当你测试一个数是否可以被另一个数整除时,你想知道余数是否为零。更改此项:

if (p / n == 0)
为此:

if (p % n == 0)
但是看起来你的循环还是有问题。您可以将其改写为:

static void FindPrime(long p)
{
    long max = (long)Math.Ceiling(Math.Sqrt(p));
    foreach (long n in list)
    {
        if (n > max) 
        {
            break;
        }
        else if (p % n == 0)
        {
            return;
        }
    }

    list.Add(p);
}

仅供参考,我这样做时得到的答案是142913828920。我得到142911828929,不知道为什么?仅供参考,我这样做时得到的答案是142913828920。我得到142911828929,不知道为什么?尝试:如果p%n==0尝试:如果p%n==0