#include <stdio.h>
#include <stdbool.h>
#include <math.h>
// Count the number of divisors of a number
int countDivisor(int n)
{
int divisors = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) { // count one divisor for square number
divisors++;
} else { // count both divisors for non-square
divisors += 2;
}
}
}
return divisors;
}
// Check if number is sphenic
bool isSphenic(int n)
{
if (n >= 30 && countDivisor(n) == 8) {
return true;
}
return false;
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (isSphenic(n) == true) {
printf("Sphenic number!");
} else {
printf("Not a sphenic number!");
}
return 0;
}
这个C代码基于蝶数正好有8个除数这一事实.如果我输入n = 56
,则输出将是Sphenic number
.我知道这是错误的,但我找不到我的错误.