题意:

素数41可以写成六个连续素数的和:

41 = 2 + 3 + 5 + 7 + 11 + 13

在小于一百的素数中,41能够被写成最多的连续素数的和。

在小于一千的素数中,953能够被写成最多的连续素数的和,共包含连续21个素数。

在小于一百万的素数中,哪个素数能够被写成最多的连续素数的和?

思路:首先打出100000以内的素数表,然后计算出所有从第一个素数到 j 的和,然后枚举两个端点判断是否符合要求即可


/*************************************************************************
> File Name: euler050.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年07月01日 星期六 19时43分19秒
************************************************************************/ #include <stdio.h>
#include <inttypes.h> #define MAX_N 1000000 int32_t prime[MAX_N + 10] = {0};
int32_t primeList[MAX_N + 10] = {0};
int64_t sum[MAX_N + 10] = {0}; void Init() {
for (int32_t i = 2 ; i <= MAX_N ; i++) {
if (!prime[i]) {
primeList[++primeList[0]] = i;
}
for (int32_t j = 1 ; j <= primeList[0] ; j++) {
if (i * primeList[j] > MAX_N) break;
prime[i * primeList[j]] = 1;
if (i % primeList[j] == 0) break;
}
}
for (int32_t i = 1 ; i <= primeList[0] ; i++) {
sum[i] = sum[i - 1] + primeList[i];
}
}
int32_t main() {
Init();
int32_t maxP = 953 , maxL = 21;
for (int32_t i = 1 ; i < primeList[0] ; i++) {
for (int32_t j = i + maxL ; j <= primeList[0] ; j++) {
if (sum[j] - sum[i] > MAX_N) break;
if (prime[sum[j] - sum[i]]) continue;
if (maxL < j - i){
maxL = j - i;
maxP = sum[j] - sum[i];
}
}
}
printf("maxL = %d , maxP = %d\n",maxL , maxP);
return 0;
}

Project Euler 50 Consecutive prime sum的更多相关文章

  1. Project Euler 87 :Prime power triples 素数幂三元组

    Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...

  2. Project Euler 77:Prime summations

    原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...

  3. Project Euler 83:Path sum: four ways 路径和:4个方向

    Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...

  4. Project Euler 82:Path sum: three ways 路径和:3个方向

    Path sum: three ways NOTE: This problem is a more challenging version of Problem 81. The minimal pat ...

  5. Project Euler 81:Path sum: two ways 路径和:两个方向

    Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom ...

  6. Project Euler 20 Factorial digit sum( 大数乘法 )

    题意:求出100!的各位数字和. /************************************************************************* > Fil ...

  7. Project Euler 16 Power digit sum( 大数乘法 )

    题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...

  8. Project Euler 56: Powerful digit sum

    一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...

  9. Project Euler Problem 7-10001st prime

    素数线性筛 MAXN = 110100 prime = [0 for i in range(210000)] for i in range(2,MAXN): if prime[i] == 0: pri ...

随机推荐

  1. elasticsearch6.4中文文档

    男朋友翻译的,我这边做一个查看入口,分享给大家,O(∩_∩)O哈哈~ 版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者. https://blog.csd ...

  2. MySQL经常使用命令--show命令使用

    log into the mysql for localhost mysql -u username -ppasswd(there is no space) for ip mysql -h ip -P ...

  3. XTU OJ 1207 Welcome to XTCPC (字符串签到题)

    Problem Description Welcome to XTCPC! XTCPC start today, you are going to choose a slogan to celebra ...

  4. IOS总结_实现UIButton的图文混排(二)

    非常久没有写博客了,之前写过一篇关于UIButton图文混排的,可是有点复杂,今天来一个比較简单地.相信大家回用得着 UIButton *button=[[UIButton alloc, , )]; ...

  5. fzu 1075 分解素因子

    代码: #include<cstdio> #include<cstring> #include<iostream> using namespace std; int ...

  6. Beta 分布归一化的证明(系数是怎么来的),期望和方差的计算

    1. Γ(a+b)Γ(a)Γ(b):归一化系数 Beta(μ|a,b)=Γ(a+b)Γ(a)Γ(b)μa−1(1−μ)b−1 面对这样一个复杂的概率密度函数,我们不禁要问,Γ(a+b)Γ(a)Γ(b) ...

  7. Hdu-6249 2017CCPC-Final G.Alice’s Stamps 动态规划

    题面 题意:给你n个集合,每个集合有L到R这些种类的邮票,让你选择其中的K个集合,使得最后选择的邮票种类尽可能多,N,L,R都<=2000 题解:容易乱想到网络流,可是再细想一下就会发现处理不了 ...

  8. js设计模式-门面模式

    适用场景:门面模式在DOM脚本编程这种需要对各种不一致的浏览器接口的环境中很常用. 例子:阻止模式事件 var DED = widow.DED || {}; DED.util = { stopProp ...

  9. 22.Generate Parentheses[M]括号生成

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  10. BZOJ 1507 splay

    写完维修数列 这不是水题嘛233333 //By SiriusRen #include <cstdio> #include <cstring> #include <alg ...