Non-abundant sums
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
题目:
如果一个数的所有真因子之和等于这个数,那么这个数被称为完全数。例如,28 的所有真因子之和为 1 + 2 + 4 + 7 + 14 = 28,所以 28 是一个完全数。
如果一个数的所有真因子之和小于这个数,称其为不足数,如果大于这个数,称其为过剩数。
12 是最小的过剩数,1 + 2 + 3 + 4 + 6 = 16。因此最小的能够写成两个过剩数之和的数字是 24。经过分析,可以证明所有大于 28123 的数字都可以被写成两个过剩数之和。但是这个上界并不能被进一步缩小,即使我们知道最大的不能表示为两个过剩数之和的数字要比这个上界小。
找出所有不能表示为两个过剩数之和的正整数之和。
这个题需要用到21题的求一个数的因子和,然后求得因子和之后这个题就简单了,先记录过剩数,这个如果21题弄懂了,那么下面的那些标记过程就一目了然,下面是代码实现:
#include <stdio.h> #include <math.h> #define MAX_N 28123 int prime[MAX_N + 5]; int cnt[MAX_N + 5]; int f[MAX_N + 5]; int d[MAX_N + 5]; int num[MAX_N + 5]; int main() { for (int i = 2; i <= MAX_N; i++) { if (!prime[i]) { prime[++prime[0]] = i; f[i] = 1 + i; cnt[i] = 1; } for (int j = 1; i * prime[j] <= MAX_N; j++) { prime[i * prime[j]] = 1; if (i % prime[j] == 0) { f[i * prime[j]] = f[i] / (1 - pow(prime[j], cnt[i] + 1)) * (1 - pow(prime[j], cnt[i] + 2)); cnt[i * prime[j]] = cnt[i] + 1; break; } else { f[i * prime[j]] = f[i] * f[prime[j]]; cnt[i * prime[j]] = 1; } } } for (int i = 2; i <= MAX_N; i++) {//标记过剩数 if (f[i] - i <= i) continue; d[++d[0]] = i; } int falg = 1; int ans = 0; for (int i = 1; i <= d[0]; i++) {//标记可以用过剩数相加得来的整数 falg = 1; for (int j = i; j <= d[0]; j++) { if ((d[i] + d[j]) > MAX_N) break; num[d[i] + d[j]] = 1;//标记可以用两个过剩之和的数 falg = 0; } if (falg) break; } for (int i = 1; i <= MAX_N; i++) { if(num[i]) continue;//是被标记的数就继续执行循环 ans += i; } printf("%d\n", ans); return 0; }
最终答案:4179871