编 将任意的输入一偶数n分解为两个素数之和,并给出各种方法。
如:
10=3+7;
10=5+5:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int prime(unsigned long); /*判断一个数是否是素数*/
int deal(unsigned long); /*打印出偶数的所有素数组合形式*/
int main()
{
unsigned long num;
clrscr();
printf("please input a even number:\n");
scanf("%lu",&num);
deal(num);
getch();
}
int deal(unsigned long num)
{
unsigned long i;
unsigned long mid_num=num/2;/*偶数的1/2*/
for(i=2;i<=mid_num;i++) /*让一个数从2到这个偶数的1/2*/
if(prime(i)==1 && prime(num-i)==1) /*判断i和num-i是否都是素数,如果是则打印出来*/
printf("num=%ld+%ld\n",i,num-i);
return 1;
}
int prime(unsigned long num)
{
unsigned long i;
unsigned contrl=(unsigned long)sqrt(num);
for(i=2;i<=contrl;i++)
if(num%i==0)
return 0;
return 1;
}