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命令操作简介及五种value数据类型
转自:https://blog.csdn.net/ty4315/article/details/52050721 Redis是使用键值存储数据,key必须是字符串value支持五种数据类型,最新版本又 ...
- Cloudera 5.8.3 SolrCloud+HDFS的索引数据备份和恢复。(需重启solr进程。)
一.备份基于HDFS的solrCloud集合数据 1.确认要备份的solr文件夹. /solr/history_customer_collection_test 2.开启HDFS快照功能. hdfs ...
- MYSQL 源码解读系列 [线程池。。] ----dennis的博客
http://blog.sina.com.cn/s/articlelist_1182000643_0_1.html
- HDU 2457
直接从root遍历扩展DP,当扩展到的字母和字符串字母相同时,不用修改,不同时,要求修改加1 注意不要扩展危险结点. #include <iostream> #include <cs ...
- Cocos Code IDE里xcodeprojectlua脚本更新
lua脚本改动后xcode须要clean又一次编译才干更新,这个是xcode里的老毛病了,网上有一些脚本但不是针对Cocos Code IDE的project文件夹的,这里列出 cocos2dx版本号 ...
- [Angular] Performance Caching Policy - Cache First, Network Last
If you want to cache API response by using angular service-worker, you can do it in: src/ngsw-config ...
- Python查询数据库,中文的结果显示不出来
表里面的数据: 问题:查询数据库,返回结果不是中文可以,是中文的话就报错UnicodeEncodeError: 'gbk' codec can't encode character '\xd4' in ...
- 【cl】selenium实例一:打开百度,获取第四个标题
/*创建类的时候是TestNG Class*/ package Selenium_lassen; import static org.junit.Assert.*; import java.util. ...
- MyBatis中sqlSession操作数据库,不报错但无法实现数据修改(增、改、删)
public void addCustomerTest() throws Exception { SqlSession sqlSession = MyBatisUtils.getSession(); ...
- Windows 10家庭版也能共享打印机(中)解除Guest账户网络登录限制,实现局域网共享
由于Windows系统默认是禁止Guest账户从网络登录的.我们须要解除这个限制.首先想到的是用组策略编辑器gpedit.msc. 可是Windows 10家庭版没有组策略编辑器,我们先尝试用U盘把W ...