欧拉计划23题

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值