欧拉计划 第三十题

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 448208 = 84 + 24 + 04 + 849474 = 94 + 44 + 74 + 44

As 1 = 14 is 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 48208 = 8 4 + 2 4 + 0 4 + 8 49474 = 9 4 + 4 4 + 7 4 + 4 4

由于1 = 1 4不是总和,因此不包括在内。

这些数字的总和是1634 + 8208 + 9474 = 19316。

找到所有可写入的数字的总和,作为其数字的五次幂的总和。

思考:

上界估算:

在这道题中,我们并不知道枚举到哪个数的时候找到五次幂的总和,所以我们需要估算上界

那么我们思考一下最大的上界是多少呢?

我们可以想到与之相关的两个函数f(n) = 10的n次方,g(n) = 9的5次方 * n;

根据图像比较两个值的范围的估算我们可以知道n = 6的时候最为合理;

上界可以定到 9的5次方 * 6;

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

#define max 354264

int32_t isfine(int32_t num){
	int32_t sum = 0, store = num;
	while(num){
		sum += (int) pow(num % 10, 5) ;
		num /= 10;
	}
	return sum == store;
}

int32_t main(){
	int32_t sum = 0;
	for(int32_t i = 2; i <= max; i++){
		if(isfine(i)) sum += i;
	}
	printf("%d\n", sum);
	
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值