Problem 50 Consecutive prime sum (线性筛)
来源:互联网 发布:清除数据恢复出厂英文 编辑:程序博客网 时间:2024/06/09 17:30
Consecutive prime sum
Problem 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?
代码:
#include<bits/stdc++.h>using namespace std;vector<int> primes;int prime[1000000];void init(){ for(int i = 0 ; i < 1000000 ; i++){ prime[i] = 1; } for(int i = 2 ; i < 1000000; i++){ if (prime[i]){ primes.push_back(i); if(i <= (int)sqrt((double)1000000)){ for(int t = i*i ; t<1000000 ; t+=i)//prime[i*i+k*i]=false{ prime[t] = 0; } } } }}int main(){ init(); int max = 0; int maxlen = 0, maxsum = 0, cursum = 0; for(int i = 2 ; i < primes.size() ; i++){ cursum = 0; for(int j = i ; j < primes.size() ; j++) { cursum+=primes[j]; if (cursum>=1000000) break; if (prime[cursum] && j - i > maxlen ) { maxlen = j-i; maxsum = cursum; } } } cout<<maxsum<<endl;; return 0;}
2 0
- Problem 50 Consecutive prime sum (线性筛)
- Project Euler:Problem 50 Consecutive prime sum
- UVa1210 - Sum of Consecutive Prime Numbers(欧拉筛法即线性筛法)
- POJ 2739 Sum of Consecutive Prime Numbers(线性素数筛+前缀和)
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Problem 7 10001st prime (线性筛)
- poj 2739 Sum of Consecutive Prime Numbers(尺取+筛素数)
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
- POJ 2739(Sum of Consecutive Prime Numbers) 素数筛法+暴力 Java
- poj 2739 Sum of Consecutive Prime Numbers【素数筛】
- 欧拉工程第50题:Consecutive prime sum
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- Android studio 快捷键及使用技巧
- C#之你该知道的东西1.0
- linux c实现文件复制
- Class类的使用——课后总结
- 《JVM学习系列》二.HotSpot虚拟机对象解密
- Problem 50 Consecutive prime sum (线性筛)
- typedef 的用法
- c语言读写函数
- android Studio Lambda插件(gradle-retrolambda)安装
- 2. 使用Python 解释器
- 拉格朗日(Lagrange)插值曲线
- IOC控制反转手记
- unity 3d中如何创建公有共用的代码段或数据,让所有其他类或者脚本都能访问?(代码说明static 静态类的使用)
- linux c 获得本机时间date