C 语言编程:发现亲和数343


在数学中,亲和数是一对自然数,其中一个数等于另一个数的真因子之和,反之亦然。换句话说,对于一对亲和数 (a, b),a 的真因子之和等于 b,而 b 的真因子之和等于 a。

例如,220 和 284 是亲和数。220 的真因子为 1、2、4、5、10、11、20、22、44、55、110,其和为 284。284 的真因子为 1、2、4、71、142,其和为 220。

在 C 语言中,我们可以编写一个程序来找出给定范围内的所有亲和数。以下是一个示例程序:
#include
#include
bool isPerfect(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum == n;
}
bool isAmicable(int n, int m) {
return isPerfect(n) && isPerfect(m) && n == m;
}
int main() {
int lower, upper;
printf("Enter the lower bound: ");
scanf("%d", &lower);
printf("Enter the upper bound: ");
scanf("%d", &upper);
printf("Amicable numbers between %d and %d:", lower, upper);
for (int i = lower; i

2024-11-05


上一篇:C 语言中从函数返回数组

下一篇:C语言数组长度函数全解