求C语言完整程序: 写一个函数验证哥德巴赫猜想;一个不小于6的偶数可以表示为两个素数之和。
时间: 2023-06-08 19:04:33 浏览: 114
c语言 验证哥德巴赫猜想
以下是C语言完整程序:
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
void verifyGoldbachConjecture(int n) {
if (n < 6 || n % 2 != 0) {
printf("Input is invalid.\n");
return;
}
for (int i = 2; i < n; i++) {
if (isPrime(i)) {
int diff = n - i;
if (isPrime(diff)) {
printf("%d + %d = %d\n", i, diff, n);
return;
}
}
}
printf("Goldbach conjecture is false!\n");
}
int main() {
int n;
printf("Enter an even number greater than or equal to 6: ");
scanf("%d", &n);
verifyGoldbachConjecture(n);
return 0;
}
阅读全文