题意:

素数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. rmq问题和lca可以相互转化

    Sparse Table算法 一般RMQ的Sparse Table(ST)算法是基于倍增思想设计的O(Nlog2N) – O(1)在线算法 算法记录从每个元素开始的连续的长度为2k的区间中元素的最小值 ...

  2. Tyvj - 1286 - 校门外的树2

    描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1 ...

  3. git 添加到环境变量

    github 新增仓库的后的提示 …or create a new repository on the command line echo "# top" >> REA ...

  4. Spring深入理解(一)

    Spring 框架的设计理念与设计模式分析 Spring核心组件 Spring 框架中的核心组件只有三个:Core.Context 和 Beans Spring 的设计理念 前面介绍了 Spring ...

  5. git笔记之eclipse使用github远程仓库进行版本号管理

    原文地址:http://dtbuluo.com/90.html 这里记录一下eclipse开发工具中git的使用说明. 环境:centOS.eclipse-jee-kepler-SR2-linux-g ...

  6. PowerPC汇编指令集

    PowerPC 体系结构规范(PowerPC Architecture Specification)公布于 1993 年,它是一个 64位规范 ( 也包括 32 位子集 ).差点儿全部常规可用的 Po ...

  7. dlopen failed: empty/missing DT_HASH in &quot;libx.so&quot; (built with --hash-style=gnu?)

    崩溃日志内容: java.lang.UnsatisfiedLinkError: dlopen failed: empty/missing DT_HASH in "libxxxx.so&quo ...

  8. phonegap(cordova) 自己定义插件代码篇(四)----读取本地图片

    有时候确实知道本地图片地址,要获取到base64  /** * 获取本地图片,包括路径和压缩后的 base64 */ (function (cordova) { var define = cordov ...

  9. 【翻译自mos文章】Oracle GoldenGate 怎么在源头的传输进程和目的端的server/collector进程之间分配 port?

    Oracle GoldenGate 怎么在源头的传输进程和目的端的server/collector进程之间分配 port? 来源于: How Does GoldenGate Allocates Por ...

  10. android监听虚拟按键的显示与隐藏【转】

    本文转载自:http://blog.csdn.net/u014583590/article/details/55263141 虚拟按键在华为手机中大量存在,而虚拟按键的存在无疑增加了屏幕适配的难度,往 ...