两个 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);
	}
}


哥德巴赫猜想是一个未解决的问题,它断言每个大于2的偶数都可以表示为两个素数之和。在C++中实现这个猜想的算法并不是直接用于计算,因为这是数学难题,无法通过简单的程序求解所有偶数的情况。通常,它用于启发式搜索或优化算法的演示,比如使用启发式方法来猜测可能的素数对。 如果你想要模拟一个简单的程序,可以创建一个函数库,用于判断给定数字是否为素数,然后编写一个函数尝试将较大的偶数分解成两部分,但这不会验证所有偶数的哥德巴赫猜想。这更像是一种练习编程技巧而非实际解决问题的方式。 下面是一个简单的C++代码示例,展示了如何检查一个数是否为素数,以及如何尝试将其拆分为两个较小的素数: ```cpp #include <iostream> #include <vector> // 判断是否为素数 bool isPrime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; ++i) if (num % i == 0) return false; return true; } // 拆分偶数为两个素数 void conjectureGoldbach(int num) { if (num <= 4) std::cout << "2 = 2 + 0" << std::endl; else { for (int i = 2; i < num; ++i) { // 只考虑比当前数小的素数 if (isPrime(i) && isPrime(num - i)) { std::cout << num << " = " << i << " + " << (num - i) << std::endl; break; } } } } int main() { int n; std::cout << "Enter an even number to test Goldbach's Conjecture: "; std::cin >> n; conjectureGoldbach(n); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值