当前位置:网站首页>2020.11.22 Exam Goldbach Conjecture Solution
2020.11.22 Exam Goldbach Conjecture Solution
2022-08-10 03:21:00 【bj_hacker】
题目
哥德巴赫猜想
【问题描述】
哥德巴赫猜想:任何大于 4 can be divided into the sum of two odd prime numbers.
比如:8=3+5.
20=3+17=7+13
42=5+37=11+31=13+29=19+23
你的任务是:验证小于 1000 000 The numbers satisfy Goldbach's conjecture.
【输入】
输入包含多组测试数据,One for each set of test data n(6<=n<1000 000).
读入以 0 结束.
【输出】
对于每组数据,输出形如 n=a+b,其中 a,b 是奇质数.若有多组满足条件的 a,b,输
出 b-a 最大的一组.若无解,输出“Goldbach’s conjecture is wrong.
思路
Ehrlich sieve method+Loop to determine another number
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int n;
bool as[maxn];
inline void init(){
as[1]=true;
for(int i=2;i<=1000000;i++){
if(as[i])continue;
for(int j=2*i;j<=1000000;j+=i)as[j]=true;
}
}
int main(){
init();
while(1){
scanf("%d",&n);
if(!n)break;
if(n%2==1){
printf("Goldbach’s conjecture is wrong\n");
continue;
}
printf("%d = ",n);
for(int i=3;i<=n;i++){
if(as[i])continue;
if(!as[n-i]){
printf("%d + %d\n",i,n-i);
break;
}
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
浅写一个下拉刷新组件
【二叉树-中等】687. 最长同值路径
中级xss绕过【xss Game】
Janus实际生产案例
MySQL:日志系统介绍 | 错误日志 | 查询日志 | 二进制日志:bin-log数据恢复实践 | 慢日志查询
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
what is a microcontroller or mcu
OpenCV图像处理学习一,加载显示修改保存图像相关函数
按钮倒计时提醒
2022 Top Net Cup Quals Reverse Partial writeup
《GB39707-2020》PDF download
2022杭电多校联赛第七场 题解
SQLserver加个判断
【Kali安全渗透测试实践教程】第7章 权限提升
LeetCode每日两题01:移动零 (均1200道)方法:双指针
【Kali安全渗透测试实践教程】第9章 无线网络渗透
【二叉树-中等】2265. 统计值等于子树平均值的节点数
OptiFDTD应用:纳米盘型谐振腔等离子体波导滤波器
2022.8.9考试独特的投标拍卖--800题解
QT模态对话框及非模态对话框学习









