数字 81 有一个特殊的性质,它的数字和的某个幂等于 81(九的平方)。八十一 (81) 是具有此属性的第一个数字(不考虑一位数字的数字)。下一个是 512。让我们看看这两种情况的详细信息
8 + 1 = 9 , 9 ** 2 = 81
512 = 5 + 1 + 2 = 8 , 8 ** 3 = 512
4913 = 4 + 9 +1 + 3 = 17 , 17 ** 3 = 4913
我们需要创建一个函数,它接收一个数字作为参数n
并返回此序列的此类型数值
示例(输入 → 输出)
1 --> 81
2 --> 512
3 --> 2401
题目难度:一般
题目来源:https://www.codewars.com/kata/55f4e56315a375c1ed000159
def power_sum_digit(n):
# your code here
assert power_sum_digit(1) == 81
assert power_sum_digit(2) == 512
assert power_sum_digit(3) == 2401
assert power_sum_digit(4) == 4913
assert power_sum_digit(5) == 5832
assert power_sum_digit(6) == 17576
xiaopeng
(霍格沃兹_小鹏)
2022 年8 月 11 日 03:28
2
def power_sumDigTerm(n):
while n > 0:
sum = 81
while True:
sum_str = str(sum)
s = 0
for i in sum_str:
s += int(i)
if s == 1:
sum += 1
continue
index = 1
while s ** index <= sum:
if s ** index == sum:
n -= 1
if n == 0:
return s ** index
index += 1
sum += 1
def power_sum_digit(n):
i=0
s=10
while i<n:
sum_s=sum([int(str(s)[i]) for i in range(len(str(s)))])
if sum_s==1:
s+=1
else:
degree=1
while sum_s**degree<=s:
if sum_s**degree==s:
i+=1
break
else:
degree+=1
s+=1
return s-1
assert power_sum_digit(1) == 81
assert power_sum_digit(2) == 512
assert power_sum_digit(3) == 2401
assert power_sum_digit(4) == 4913
assert power_sum_digit(5) == 5832
assert power_sum_digit(6) == 17576