Project Euler 50 Consecutive prime sum
题意:
素数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的更多相关文章
- Project Euler 87 :Prime power triples 素数幂三元组
Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- 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 ...
- 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 ...
- 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 ...
- Project Euler 20 Factorial digit sum( 大数乘法 )
题意:求出100!的各位数字和. /************************************************************************* > Fil ...
- Project Euler 16 Power digit sum( 大数乘法 )
题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...
- Project Euler 56: Powerful digit sum
一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...
- 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 ...
随机推荐
- redis-windows上的安装与其他命令
为什么用Redis 数据库的IO是一个性能瓶颈,需要用redis来解决,100个IO并发已经很不错了,因为数据库天生就需要写磁盘,而redis不需要实时写磁盘而又可以存入数据库 安装 以服务的方式启动 ...
- sqldependency类轮询功能
System.Data.SqlClient.SqlDependency类为我们提供了一个关于sql2005的很好的功能 ,虽然这个东西限制有很多很多,但还是有很实用价值的. 我们先看一个演示例子: 例 ...
- Codeforces 525E Anya and Cubes 中途相遇法
题目链接:点击打开链接 题意: 给定n个数.k个感叹号,常数S 以下给出这n个数. 目标: 随意给当中一些数变成阶乘.至多变k个. 再随意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制 ...
- BZOJ 1044 HAOI2008 木棍切割 二分答案+动态规划
题目大意:给定n个连在一起的木棍.分成m+1段.使每段最大值最小,求最大值的最小值及最大值最小时切割的方案数 第一问水爆了--二分答案妥妥秒过 第二问就有些难度了 首先我们令f[i][j]表示用前j个 ...
- [Unit Testing] Set the timeout of a Test in Mocha
Mocha uses a default timeout of 2000 ms. However, if for some reason that does not work for your use ...
- python+Android+uiautomator的环境
Python+Android+uiautomator的环境搭建 Python 下载适合系统的版本并安装,安装时勾选把路径加入path 验证:windows下打开cmd输入python 出现以下界面说明 ...
- 【SQLSERVER】MD5注意事项
sql中使用MD5加密是很常见的事情,但是不知道注意点的人还是会即便是拷贝网络上的写法也是会出现错误的. 举个例子简单说明: 由上图我们可以发现相同的字符串但是得到的MD5加密的字符却是不相同的,那么 ...
- WebP 文件及其编码解码工具(WebPconv)
1. webp 文件 与JPEG相同,WebP 是一种有损压缩利用预测编码技术. WebP 是 Google 新推出的影像技术,它可让网页图档有效进行压缩,同时在质量相同的情况下,WebP 格式图像的 ...
- 框架-Java:Spring MVC
ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...
- springboot与mybatis集成
1)添加依赖 <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId& ...