检查一个数是否能表示成2个质数之和,并且将所有组合输出出来。
Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool Function(int n){//将传过来的值进行质数判定,
int num = sqrt(n);//对n进行开平方根
if(n%2 == 0 && n==1){
return false;
} else{
for (int i = 3; i <= num; i++) {//循环判定
if(n%i == 0){
return false;
}
}
return true;
}
}
void Function1(int num){
bool Function(int n);
bool isPrime1 = false,isPrime2 = false;
int half = num / 2;
for (int i = 1; i <= half; ++i) {
isPrime1 = Function(i);
isPrime2 = Function(num - i);
if(isPrime1 && isPrime2){//i和num - i均为质数时则进行输出
printf("%d = %d + %d \n",num,i,(num - i));
}
}
}
int main(){
int num ;
scanf("%d",&num);
Function1(num);
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/9ae16384ff9eabcaaaef27d40ac0f88d.png)
(如有不正之处,请不吝赐教QWQ)