#include <stdio.h>
#include <math.h>
int primes[1001] = {0};
int getPrimesCount() {
int count = 0;
for (int i = 2; i <= 1000; i++) {
if (isPrime(i)) {
primes[count++] = i;
}
}
return count;
}
int isPrime(int num) {
if (num == 2 || num == 3 || num == 7) {
return 1;
} else if (num % 6 != 1 && num % 6 != 5) {
return 0;
} else {
int temp = sqrt(num);
for (int i = 5; i <= temp; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
}
return 1;
}
}
int main() {
int num, temp, count;
scanf("%d", &num);
temp = num / 2;
count = getPrimesCount();
for (int i = 0; i < count && primes[i] <= temp; i++) {
for (int j = 0; j < count && primes[j] < num; j++) {
if (primes[i] + primes[j] == num) {
printf("%d, %d\n", primes[i], primes[j]);
}
}
}
printf("\n");
return 0;
}
给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。
最新推荐文章于 2021-05-21 01:27:32 发布