project-euler/119_digit_power_sum.py

35 lines
675 B
Python

#!/usr/bin/env python3
# quick and dirty solution
from math import sqrt, log10
def main(n):
a = 2
nums = []
while len(nums) < n or a < nums[-1]**(1.0/pw):
an, pw = check(a, nums*(len(nums)>=n))
a += 1
if an:
nums += an
nums = sorted(nums)[:n]
return nums[-1]
def check(a, nums):
pw = int((a/9) * (1/log10(a)))
pw = 2 if pw<2 else pw
n = a**pw
r = []
while len(str(n)) <= a and (not nums or n < nums[-1]):
if digsum(n) == a:
r.append(n)
n *= a
return r, pw
def digsum(n):
return sum(map(int, str(n)))
#print(main(2))
#print(main(10))
print(main(30))