素数筛法-水题

问题 G: Goldbach's Conjecture

时间限制1 Sec  内存限制32 MB
提交9  解决9
[
提交][状态][讨论版]

题目描述

Goldbach's Conjecture: For any evennumber n greater than or equal to 4, there exists at least one pair of primenumbers p1 and p2 such that n = p1 + p2. 
This conjecture has not been proved nor refused yet. No one is sure whetherthis conjecture actually holds. However, one can find such a pair of primenumbers, if any, for a given even number. The problem here is to write aprogram that reports the number of all the pairs of prime numbers satisfyingthe condition in the conjecture for a given even number.

A sequence of even numbers is given asinput. Corresponding to each number, the program should output the number ofpairs mentioned above. Notice that we are interested in the number ofessentially different pairs and therefore you should not count (p1, p2) and(p2, p1) separately as two different pairs.

 

输入

An integer is given in each input line.You may assume that each integer is even, and is greater than or equal to 4 andless than 2^15. The end of the input is indicated by a number 0.

 

输出

Each output line should contain aninteger number. No other characters should appear in the output.

 

样例输入

4

10

16

0

样例输出

1

2

2

 

#include <stdio.h>
#include <math.h>
#define N 100000
int a[N];
void getprime()
{
	int i, j;
	a[1]=0;
	a[2]=1;
	for(i=3; i<=N; i++)
	{
		if(i%2==0)
			a[i]=0;
		else
			a[i]=1;
	}
	int t=sqrt(N);
	for(i=3; i<=t; i++)
	{
		if(a[i])
		{
			for(j = 2*i; j<=N; j+=i)
				a[j]=0;
		}
	}
}
int main()
{
	int i, count,n;
	getprime();
	while(scanf("%d", &n),n)
	{
		for(count =0, i=2; i<=n/2; i++)
		{
			if(a[i]&&a[n-i])
				count++;
		}
		printf("%d\n", count);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值