[c]代码库
/* 证明歌德巴赫猜想 */
#include <stdio.h>
#include <math.h>
int input_data(void) {
int data = 0;
do {
printf("Please input a even integer (>=6):");
scanf("%d", &data);
} while (data < 6 || data % 2 == 1);
return data;
}
int is_prime(const int data) {
int i = 0;
int max = sqrt(data);
for (i = 2; i <= max; ++i) {
if (data % i == 0)
return -1;
}
return 0;
}
int goldbach_conjecture(const int n) {
int a = 0;
int mid = n / 2;
for (a = 3; a <= mid; a += 2) {
if (0 == is_prime(a)) {
if (0 == is_prime(n - a)) {
printf("%d can be devided into two prime number: %d and %d\n",
n, a, n - a);
return 0;
}
}
}
return -1;
}
int main(void) {
int n = 0;
int flag = 0;
/* 输入一个正整数 */
n = input_data();
/* 证明猜想 */
flag = goldbach_conjecture(n);
if (0 == flag) {
printf("The Goldbach's conjecture is correct!\n");
} else {
printf("The Goldbach's conjecture is wrong!\n");
}
return 0;
}