欧拉计划30题

Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634=1^4+6^4 +3^4+4^4\\ 8208=8^4+2^4+0^4+8^4\\ 9474=9^4+4^4+7^4+4^4

As 1= 1^4is not a sum it is not included.

The sum of these numbers is 1634+8208+9474=19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

各位数字的五次幂

令人惊讶的是,只有三个数可以写成其各位数字的四次幂之和:
1634=1^4+6^4 +3^4+4^4\\ 8208=8^4+2^4+0^4+8^4\\ 9474=9^4+4^4+7^4+4^4

由于1=1^4并不是求和,所以这里不计入内。

上面这三个数的和是1634+8208+9474=19316。

找出所有可以写成其各位数字的五次幂之和的数,并求这些数的和。

         这个题不难,把每位数提出来再通过5的幂次再进行相加起来和这个数本身对比就可以了,但是有个问题就是这个上界去如何得到?

        先通过发现每位数最大为9,那么就是每位最大就是9^5,设有n位,那么这个数的每位数的5幂次和最大就为n*9^5,而有n位的话,这个数就得大于等于10^n,通过这两个条件利用二分法,去得到n最大为6点几

        那就取n=7,上界最大就为7*9^5=413343,最终选择了400000, 然后开始写代码:

#include <stdio.h>
#include <math.h>
#define MAX_N 400000

int is_perfact_num(int x) {
    int s = x, num = 0;
    while (s) {
        num += pow((s % 10), 5);//将每一位的5幂次加上 
        s /= 10;
    }
    return num == x;//返回数每一位的5幂次和是否与数本身相等
}



int main() {
    int ans = 0;
    for (int i = 2; i < MAX_N;i++) {
        if (!is_perfact_num(i)) continue;
        ans += i;
    }
    printf("%d\n", ans);
    return 0;
}

最终答案:443839 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

初猿°

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

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

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

打赏作者

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

抵扣说明:

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

余额充值