当前位置:网站首页>Goldbach`s Conjecture
Goldbach`s Conjecture
2022-06-28 08:10:00 【Angeliaaa】
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:
Every even integer, greater than 2, can be expressed as the sum of two primes [1].
Now your task is to check whether this conjecture holds for integers up to 107.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).
Output
For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where
1) Both a and b are prime
2) a + b = n
3) a ≤ b
Sample Input
2
6
4
Sample Output
Case 1: 1
Case 2: 1
Note
1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13...
题意:给出几组测试数据,每组给出一个n,问n能被分成几对素数的和。
思路:先进性一个素数打表,把数据范围内所有素数存在一个数字内,当然此时已经是从小到大排好的了,然后从数组中的第一个1到最后一个便利,如果n减去该元素的值还是一个素数的话num++,如果该元素大于等于n/2+1,结束便利。输出num的值即可。代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool a[10000001];
int prime[666666];
int main()
{
int i,j,h,n,T,num,k=1,l=0;
for(i=2; i<=10000000; i++)
{
if(a[i]==false)
{
prime[l++]=i;
for(j=i+i; j<=10000000; j=j+i)
a[j]=true;
}
}
a[0]=a[1]=true;
scanf("%d",&T);
while(T--)
{
num=0;
scanf("%d",&n);
for(i=0; i<l; i++)
{
if(prime[i]>=n/2+1)
break;
h=n-prime[i];
if(a[h]==false)
{
num++;
}
}
printf("Case %d: %d\n",k++,num);
}
return 0;
}
边栏推荐
- Prometheus + grafana + MySQL master-slave replication + host monitoring
- Prometheus deployment alarm docking QQ mailbox
- Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
- 图像翻译:UVCGAN: UNET VISION TRANSFORMER CYCLE-CONSISTENT GAN FOR UNPAIRED IMAGE-TO-IMAGE TRANSLATION
- About ASM disk space full, clean up ASM disk
- 【js】-【DFS、BFS应用】-学习笔记
- 2022巴黎时装周儿童单元6.19武汉站圆满落幕
- MySQL single table access method
- 开户券商怎么选择?网上开户是否安全么?
- js运算符的优先级
猜你喜欢

B_QuRT_User_Guide(26)
![[learning notes] shortest path + spanning tree](/img/38/42b7e321389e3a47304fca5c37a2cf.png)
[learning notes] shortest path + spanning tree

About using font icons in placeholder

Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)

Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde

Eslint syntax monitoring off

Redis persistence problem and final solution

MySQL implements transaction persistence using redo logs

抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德

你了解TCP协议吗(一)?
随机推荐
Is it reliable for the top ten securities companies to register and open accounts? Is it safe?
Configuring multiple instances of MySQL under Linux
块级元素上下左右居中的两个小技巧
Prometheus deployment alarm docking QQ mailbox
NPM clean cache
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
Prometheus service discovery
Three step problem of leetcode
安装nrm后,使用nrm命令报错internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
Set<String>
In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde
Unity gets the coordinate point in front of the current object at a certain angle and distance
[learning notes] linear basis
同花顺注册开户靠谱吗?安全吗?
PC端隐藏滚动条
Installing MySQL under Linux
App automated testing appium Tutorial Part 1 - advanced supplementary content
Software testing and quality final review
Selenium reptile
ROS notes (08) - definition and use of service data