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;
}