题目119:考察可以写成各位数之和的幂的数字
本帖最后由 欧拉计划 于 2016-8-22 22:37 编辑Digit power sum
The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.
题目:
512 是有趣的,因为他正好等于他的各位数字之和的幂: 5 + 1 + 2 = 8,83 = 512。另外一个有这个性质的数字是 614656=284 。
我们定义 an 为这个数列中的第 N 个元素,当然,起码得是两位数以上,才能求和。
已知 a2=512,a10=614656
请找出 A30。 248155780267521,是对的吗? 逝痕 发表于 2016-8-21 13:30
248155780267521,是对的吗?
我也是这结果248155780267521 代码如下
result = []
for i in range(2,100):
for j in range(2,100):
number = i**j
number = str(number)
length = len(number)
s = 0
num = []
for k in range(length):
num.append(int(number))
s += num
if s == i:
result.append(i**j)
result.sort()
a = result
print(a)
Estein 发表于 2016-8-21 20:46
代码如下
python果然简洁明了{:10_257:} 逝痕 发表于 2016-8-21 13:30
248155780267521,是对的吗?
#include <iostream>
#include <climits>
#include <algorithm>
#include <vector>
bool interesting(unsigned long long x); //判断数字是否符合要求
int main() {
std::vector<unsigned long long> arr;
for (int i = 2; i < 180; i++) {
unsigned long long temp = i;
while (temp < ULLONG_MAX / i) {
if (temp > 10){
arr.push_back(temp);
}
temp *= i;
}
}
sort(arr.begin(), arr.end());
std::vector<unsigned long long>::iterator pos = unique(arr.begin(), arr.end());
arr.erase(pos, arr.end()); //除去重复的数字
int k = 1;
for (std::vector<unsigned long long>::iterator itr = arr.begin(); itr != arr.end(); itr++){
if (interesting(*itr)) {
std::cout << "a[" << k << "] = " << *itr << std::endl;
k++;
}
}
system("PAUSE");
return 0;
}
bool interesting(unsigned long long x) {
unsigned int sum = 0;
unsigned long long temp = x;
while (temp != 0){
sum += temp % 10;
temp /= 10;
}
if (sum == 1) {
return false;
}
while (x != 1) {
if (x % sum != 0){
return false;
}
x /= sum;
}
return true;
} 对于不支持大数运算的语言那简直就是灾难 逝痕 发表于 2016-8-22 13:19
结果是对的。但缺少数学上的证明这样做已经可以了。
页:
[1]