Write a C function named isPrime that checks whether a given number is a prime or not. Include the fuction isPrime in a working program to verify Goldbach's Conjecture: Every even number greater than 4 can be written as the sum of two odd prime numbers. Make sure the function isPrime is called from the main method, and correctly returns a value to the main method.
时间: 2024-03-17 21:42:42 浏览: 14
Sure, here is a C program that includes the `isPrime` function to verify Goldbach's Conjecture:
```
#include <stdio.h>
int isPrime(int num);
int main() {
int num, i, j;
printf("Enter an even number greater than 4: ");
scanf("%d", &num);
if (num <= 4 || num % 2 != 0) {
printf("Invalid input!\n");
return 0;
}
for (i = 3; i <= num / 2; i += 2) {
if (isPrime(i) && isPrime(num - i)) {
printf("%d = %d + %d\n", num, i, num - i);
}
}
return 0;
}
int isPrime(int num) {
int i;
if (num < 2) {
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
```
The `isPrime` function takes an integer as input and returns 1 if the number is prime, and 0 otherwise. The `main` function prompts the user to enter an even number greater than 4, and then checks whether the input is valid. If the input is valid, the program uses a for loop to check all odd prime numbers less than or equal to half of the input number. For each odd prime number `i`, the program checks whether the difference between the input number and `i` is also a prime number. If both numbers are prime, the program prints out the input number as the sum of `i` and the difference between the input number and `i`. If no such pairs of prime numbers are found, the program prints nothing. The `isPrime` function is called from the `main` function to check whether each number is prime.