Project Euler 46 Goldbach's other conjecture( 线性筛法 )
题意:
克里斯蒂安·哥德巴赫曾经猜想,每个奇合数可以写成一个素数和一个平方的两倍之和
9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12
最终这个猜想被推翻了。
最小的不能写成一个素数和一个平方的两倍之和的奇合数是多少?
思路:用线性筛法记录下来所有素数,然后去生成在范围内的哥德巴赫数字即可
/*************************************************************************
> File Name: euler046.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月29日 星期四 12时51分48秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#define MAX_N 100000
int32_t IsPrime[MAX_N + 10] = {0} , primeList[MAX_N + 10] = {0};
int32_t GoldbachNum[MAX_N + 10] = {0};
void Init() {
for (int32_t i = 2 ; i <= MAX_N ; i++) {
if (!IsPrime[i]) { primeList[ ++primeList[0] ] = i; }
for (int32_t j = 1 ; j <= primeList[0] ; j++) {
if (i * primeList[j] > MAX_N) break;
IsPrime[i * primeList[j]] = 1;
if (i % primeList[j] == 0) break;
}
}
for (int32_t i = 1 ; i <= primeList[0] ; i++) {
for (int32_t j = 1 ; true ; j++) {
if (primeList[i] + 2 * j * j > MAX_N) break;
GoldbachNum[primeList[i] + 2 * j * j] = 1;
}
}
}
int32_t main() {
Init();
for (int32_t i = 33 ; i <= MAX_N ; i += 2) {
if (!IsPrime[i]) continue;
if (!GoldbachNum[i]) {
printf("ans = %d\n",i);
break;
}
}
printf("end\n");
return 0;
}
Project Euler 46 Goldbach's other conjecture( 线性筛法 )的更多相关文章
- (Problem 46)Goldbach's other conjecture
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- IEEEXtreme 10.0 - Goldbach's Second Conjecture
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Goldbach's Second Conjecture 题目来源 第10届IEEE极限编程大赛 https ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
随机推荐
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- xtrabackup增量备份mysql +MHA
http://blog.csdn.net/yanggd1987/article/category/2214421 https://www.centos.bz/2013/09/innobackupex- ...
- 今天玩了tensorflow playground,太好玩了
先上地址: http://playground.tensorflow.org 我试了一个最复杂的,螺旋形的.开始怎么训练都不行.后来我多加了几个神经元,居然能训练成功了.真是太牛逼了!
- 关于在linux下出现stdio.h文件不存在等gcc标准库不能找到的解决的方法
首先说明一下我的系统配置:ubuntu 12.04 gcc 4.6.3 有几天没有使用ubuntu了,今天拿出来编程序,刚開始编译一个uboot1.1.6的代码.出现了stdio.h:没有那么 ...
- SQL优化(SQL TUNING)之10分钟完毕亿级数据量性能优化(SQL调优)
前几天.一个用户研发QQ找我,例如以下: 自由的海豚. 16:12:01 岛主,我的一条SQL查不出来结果,能帮我看看不? 兰花岛主 16:12:10 多久不出结果? 自由的海豚 16:12:17 多 ...
- Win8下建立shortcut到開始界面
在win8前建立開始菜单都非常easy,但到win8就有点不一样了.它的開始菜单是metro风格的.以下我们来看下详细的实现代码.有兴趣的朋友能够自己測试下,它的作用是设置shortcut到metro ...
- matlab7安装后的常见问题
1.有时候.打开MatLab7时,会弹出"找不到指定的模块"对话框,如图(1)所看到的: 图(1) 找不到指定模块 产生这个问题的解决办法是.你的BLAS_VERSION环境变量没 ...
- (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...
- cxf调用WebService
一.用CXF调用WebService的几种方式,参考: http://cxf.apache.org/docs/how-do-i-develop-a-client.html 二.JaxWsProxyFa ...
- 杂项:DS(目录服务)
ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...