c 分拆素数和

Problem Description
把一个偶数拆成两个不同素数的和,有几种拆法呢?

Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。

Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。

Sample Input
30
26
0

Sample Output
3
2

代码:

#include<stdio.h>
#include<math.h>
int judge(int x);
void main()
{
	int i,n,sum;
	while(~scanf("%d",&n)){
		if(n==0){
		    break;
		}
	    sum = 0;
		for(i=2;i<n-i;i++){
		    if(judge(i)&&judge(n-i)){
			//	printf("%d+%d\n",i,n-i);
				   sum++;
			}
		}
		printf("%d\n",sum);
	}
}

int judge(int x){
	int i;
	if(x==2){
		return 1;
	} 
	for(i=2;i<=sqrt(x);i++){
		if(x%i==0){
			   return 0;
		}
	}
	return 1;
}



猜你喜欢

转载自blog.csdn.net/qq_40811682/article/details/88201539