用函数编程,一个自然数是素数,且它的数字经过任意对换后仍为素数,则称为绝对素数。如13,打印出两位数中所有的绝对素数。c语言代码
时间: 2024-03-06 07:50:58 浏览: 123
```c
#include <stdio.h>
int is_prime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) return 0;
}
return 1;
}
int is_permutation_prime(int num) {
int digits[10] = {0};
while (num > 0) {
digits[num % 10]++;
num /= 10;
}
for (int i = 2; i <= digits[1]; ++i) {
int permutation = i;
for (int j = 2; j <= 9; ++j) {
if (digits[j] > 0) {
permutation = permutation * 10 + j;
}
}
if (!is_prime(permutation)) {
return 0;
}
}
return 1;
}
void print_absolute_primes(int start, int end) {
for (int i = start; i <= end; ++i) {
if (is_prime(i) && is_permutation_prime(i)) {
printf("%d是绝对素数\n", i);
}
}
}
int main() {
print_absolute_primes(10, 99);
return 0;
}
```
阅读全文