Goldbach's Conjecture
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9338 Accepted: 5452
Description
For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that
n = p1 + p2
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. There can be many such numbers. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 215. The end of the input is indicated by a number 0.
Output
Each output line should contain an integer number. No other characters should appear in the output.
Sample Input
6
10
12
0
Sample Output
1
2
1
Source
哥德巴赫猜想
时间限制:1000MS内存限制:65536K
总提交:9338:5452
描述
对于任意偶数数n大于或等于4,存在至少一对素数p1的和p2,使得
N = P1 + P2
这种推测还没有得到证实,也遭拒绝。没有人知道这个猜想是否实际持有。然而,我们可以找到这样的一对素数,如果有的话,对于一个给定的偶数。这里的问题是写一个程序,报告的数量,对满足条件的素数猜想的一个给定的偶数。
偶数序列作为输入。可以有很多这样的数字。对应于每一个数字,程序应该输出上述的对的数目。请注意,我们有兴趣在本质上的不同对数,因此,你不应该指望(P1,P2)和(P2,P1)分别作为两个不同的对。
输入
在每个输入行给定的整数。你可以假设每个整数为偶数时,大于或等于4且小于215。由数字0表示输入结束。
产量
每一条输出线应包含一个整数。没有其他的字符应该出现在输出。
采样输入
6
10
12
0
样本输出
1
2
1
源
#include<stdio.h>
#include<math.h>
long isprime(long n)/*是素数返回1,不是返回0*/
{
long i;
for(i=2;i<=sqrt(n);i++)
{if(n%i==0)break;}
if(i>sqrt(n)) return 1;
else return 0;
}
main()
{
long p,q,m;
printf("Plesae input a even:");
while(1)
{
scanf("%ld",&m);
if(m%2!=0)printf("Please input a even:\n");
else break;
}
if(m==4){printf("4=2+2");getchar();getchar();return 0;}
for(p=3;p<=m/2;p=p+2)
{
q=m-p;
if(isprime(p)&&isprime(q))printf("%ld=%ld+%ld\n",m,p,q);
}
getchar();
getchar();
}