【问题标题】:Consecutive Prime Sum连续素数
【发布时间】:2014-09-06 01:53:48
【问题描述】:

我正在解决Project Euler 上的第 50 个 问题。

问题是:

找出一百万以下的素数,可以写成连续素数之和。

例如,41 = 2 + 3 + 5 + 7 + 11 + 13

41 是可以写成最连续素数之和的素数。

我写了一个代码来找到1000下面的素数,可以写成最连续素数的总和,检查我的代码是否找到了可以写成总和的素数(9531000 以下最连续的素数。这是我想出的:

#!/usr/bin/python

import prime

p = prime.genprimes(1000)
prms = [i for i in p]

for prm in prms:
    count = 0
    p = prm
    temp = []
    for a in prms:
        p -= a
        temp.append(a)
        count += 1
        if p == 0:
            print prm, '\t', count, '\t', temp

prime.py:

#!/usr/bin/python

def genprimes(limit):
    """
    Returns the prime numbers(generator) until the limit(inclusive) given.
    """

    D = {}
    q = 2

    while q <= limit:
        if q not in D:
            yield q
            D[q * 2] = [q]
        else:
            for p in D[q]:
                D.setdefault(p + q, []).append(p)
            del D[q]
        q += 1

运行代码时的输出:

2       1       [2]
5       2       [2, 3]
17      4       [2, 3, 5, 7]
41      6       [2, 3, 5, 7, 11, 13]    # Longest sum of consecutive primes that adds to a prime below 100
197     12      [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
281     14      [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]

问题是它没有找到素数953,它是加到1000下面的素数上的最长连续素数之和。


所以,我更改了我的代码以解决当 prmfor 循环中为 953 时的问题:

#!/usr/bin/python

import prime

p = prime.genprimes(1000)

prms = [i for i in p]

found = []

for prm in prms:
    if prm == 953:
        p = prm
        for a in prms:
            print p, '\t', a
            p -= a
            if p < -100:
                break

输出:

953     2
951     3
948     5
943     7
936     11
925     13
912     17
895     19
876     23
853     29
824     31
793     37
756     41
715     43
672     47
625     53
572     59
513     61
452     67
385     71
314     73
241     79
162     83
79      89
-10     97

知道我在这里做错了什么吗?感谢您的帮助。

【问题讨论】:

  • 素数 953 是从 7 到 89 的 21 个素数之和。

标签: python primes


【解决方案1】:

你的循环总是从索引 2 开始。看起来连续的素数不一定需要从素数 2 开始。你需要改变连续相加从哪个素数开始。

【讨论】:

    【解决方案2】:

    一个较小的例子:如果你找到最大的和小于 10 的连续素数,那么这是3 + 5 = 8,而不是2 + 3 = 5

    可能不是(也不是)通过将所有从 2 开始的素数相加总是得到最大和的情况。

    【讨论】:

      【解决方案3】:

      这个问题是在 TCS CodeVita 2016 中提出的

      #include<iostream>
      using namespace std;
      int main(){
              long long int num=0;
              cout<<"Enter the Size to count Prime number till NUM : ";
              cin>>num;
              long long int ary[num],j=2;
              ary[0] =2,ary[1]=3;
              for(int i=2;i<=num;i++){      // loop will add the prime number till num
                      if(i%2 != 0 && i%3 != 0){
                          ary[j] = i;
                              j++;
                      }
              }
              long long int k,sum=0,count=0;
              cout<<"Sum of Consecutive Prime numbers from "<<2<<" to "<<num<<endl;
              for(int i=0;i<=j;i++){
                      for(k=0;k<j;k++){
                              sum+= ary[k];
                              if(sum %2 !=0 && sum%3!=0 && sum<=num){
                                      count++;
                                  cout<<sum<<endl;
                              }
                      }
              }
              cout<<"Total Consecutive Count : "<<count<<endl;
      }
      

      输出

      样本输出 1

      Enter the Size to count Prime number till NUM : 20
      Sum of Consecutive Prime numbers from 2 to 20
      5
      17
      Total Consecutive Count : 2
      

      样本输出 2

      Enter the Size to count Prime number till NUM : 100
      Sum of Consecutive Prime numbers from 2 to 100
      5
      17
      41
      77
      Total Consecutive Count : 4
      

      【讨论】:

        猜你喜欢
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多