两个 Goldbach's Conjecture 问题

Goldbach's Conjecture
Time Limit: 1000MS   Memory Limit: 65536KByte   64 IO Format:%I64d & %I64u
Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the

 following conjecture: 

Every even number greater than 4 can be 
written as the sum of two odd prime numbers.
For example: 
8 = 3 + 5. Both 3 and 5 are odd prime numbers. 
20 = 3 + 17 = 7 + 13. 
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write

 it on the margin of this page.) 

Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million. 
Input
The input will contain one or more test cases. 
Each test case consists of one even integer n with 6 <= n < 1000000. 
Input will be terminated by a value of 0 for n.
Output

For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should

 be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding

 up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's

 conjecture is wrong."

Sample Input
8
20
42
0
Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37



#include<stdio.h>
#include<math.h>
int judge_prime(int x)
{
	if(!(x % 2))
		return 0;//是大于2的偶数必定不是素数,直接返回0; 
	for(int i = 3; i * i <= x; i+= 2)//奇数,能除尽非本身的奇数,这样的奇数一定不是素数 
	{
		if(!(x % i))
			return 0;
	}
	return 1;
}
int main()
{
	int num, ans, flag, i, j;
	while(scanf("%d",&num), num)
	{
		flag = 0;
		for(i = 3; i * 2 <= num; i+= 2)//奇素数 
		{
			if(judge_prime(i) && judge_prime(num - i))
			{
				printf("%d = %d + %d\n", num, i, num - i);
				flag = 1;
				break;
			}
		}
		if(!flag)
			printf("Goldbach's conjecture is wrong.\n");
	}
}




Goldbach's Conjecture

Time Limit: 1000MS   Memory Limit: 65536KByte   64 IO Format:%I64d & %I64u
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 (p1p2) and (p2p1) 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




#include<stdio.h>
#include<math.h>
#define Max 32769
int prime[Max] = {0}, plen;
void getprime()
{
	int temp, j, i, isprime;
	plen = 1;
	prime[0] = 2;
	for(i = 3; i < Max; i+= 2)
	{
		isprime = 1;
		for(j = 0; j < plen; j++)
		{
			if(i % prime[j] == 0)
			{
				isprime = 0;
				break;
			}
		}
		if(isprime)
			prime[plen++] = i;
	}
}
int main()
{
	int i, j, num, ans;
	getprime();
	while(scanf("%d",&num), num)
	{
		ans = 0;
		i = 0;
		j = plen - 1;
		for(; i <= j;)
		{
			if(prime[i] + prime[j] == num)
			{
				i++;
				j--;
				ans++;
			}
			else if(prime[i] + prime[j] > num)
				--j;
			else 
				i++;
		}
		printf("%d\n",ans);
	}
}


猜你喜欢

转载自blog.csdn.net/u013780740/article/details/40041147