求连续素数之和

Finding sum of consecutive primes

我正在努力提高我的 C# 技能,在此过程中我正在尝试解决 Project Euler 上的一些问题,在本例中是第 50 个问题。问题状态:

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one- hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

看起来很简单。我写了一个方法来判断某物是否是素数,列出了 100 万以下的素数(这很容易超出我的需要,但我不知道我实际需要多少),然后遍历该列表以找到质数之和。这是我的代码:

public static void Main()
    {
        IEnumerable<int> primes = Enumerable.Range(0, 1000000)
            .Where(i => isPrime(i));

        int sum = 0;
        List<int> history = new List<int>();
        foreach (int bar in primes)
        {
            if (sum + bar < 1000000)
            {
                sum += bar;
                Console.WriteLine(sum);
                history.Add(bar);



            }

        }
        while (!isPrime(sum))
        {
            sum -= history[history.Count - 1];
            history.Remove(history[history.Count - 1]);
        }
        Console.WriteLine(sum);
        Console.ReadLine();
    }



    public static bool isPrime(int num)
    {
        if (num <= 1)
        {
            return false;
        }
        else if (num == 2)
        {
            return true;
        }
        else if (num % 2 == 0)
        {
            return false;
        }
        else
        {
            var boundary = (int)Math.Floor(Math.Sqrt(num));

            for (int i = boundary; i > 1; i--)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

    }

如果我是对的,这应该求出我的质数之和高达一百万,然后减去质数直到和本身是质数。当我 运行 这个时,代码总和为 997661,但那不是质数。我减去最近添加的素数,直到得到 958577 的结果,这是素数,但这不是正确答案。我相当确定我找到素数的方法是正确的,但我无法弄清楚是什么导致我的答案错误。更糟糕的是,我不知道正确答案,所以我无法回头看看是什么导致了这个问题。

我怀疑我的 while 循环内部可能有问题,比如我可能从列表中删除了错误的值。如果有人能对我的程序无法运行的原因提供一些见解,我将不胜感激。

  1. 找到总和小于 1000000 的最长质数列表。这是从 2 开始并尽可能高的列表。令这个列表的长度为L.

  2. 现在,遍历所有总和小于1000000的列表,从长度为L的列表开始,然后是所有长度为L-1的列表,然后是L-2,等等

  3. 得到素数就停止

在 1000000 附近的每 15 个整数中大约有 1 个是素数,因此您不必检查很多列表,当然您应该通过在末端添加和删除素数而不是重新计算整个和来制作后续列表。

好吧,我正在提高我的 python 技能并使用 python.I 解决了这个问题,我认为这个问题可能是错误的,我做了计算 manually.well 这是我的回答

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

解决方法:- 我试着一步一步地解决它,这是我对我的假设的证明。

import sympy
sum=0
lst1=[]

for num in range(1,100):
    #isprime(n):return True when the num is prime and false when the num is composite
    if sympy.isprime(num) is True:
        sum+=num
        lst1.append(sum)

print("The sum list 1 is: ",lst1)

lst2=[]
for sum in lst1:
    if sum<100:
        if sympy.isprime(sum)==True:
            lst2.append(sum)
print("The list 2 is: ",lst2)
print("The required answer is :",max(lst2))

与one-hundred下方的素数相加的最长连续素数之和为41 enter image description here 所以类似地,我将限制从 100 更改为 1000,如下所示..

    import sympy
sum=0
lst1=[]

for num in range(1,1000):
    #isprime(n):return True when the num is prime and false when the num is composite
    if sympy.isprime(num) is True:
        sum+=num
        lst1.append(sum)

print("The sum list 1 is: ",lst1)

lst2=[]
for sum in lst1:
    if sum<1000:
        if sympy.isprime(sum)==True:
            lst2.append(sum)
print("The list 2 is: ",lst2)
print("The required answer is :",max(lst2))

enter image description here 这里的答案不同,给了我 281 是最高的,而在问题中提到 953 是实际答案。 只需将您的上限从 1000 更改为 1000000 我们得到的答案是 958577 enter image description here

说明:当你手动加起来时,你会得到 963 而不是 953,这使得 963 成为一个复合 number.Therefore,我认为这个问题有一个错误并且也许开发者给出了错误的答案。