# Project Euler #30: Digit Nth powers

# Project Euler #30: Digit Nth powers

+ 2 comments dic = {3: 1301,4: 19316,5: 443839,6: 548834} print(dic[int(input().strip())])

O(1) solution Lol :p

+ 6 comments The comments so far don't really indicate the best approach to this problem.

Doing a brute-force under a limit is very inefficient. You might be able to pass all test cases this way by tweaking with the upper limit, but that's only because the constrants on this problem are far too low. The constraints should be closer to 3 <= N <= 11.

Rather than doing a brute-force, it's best to turn this into a permutations problem. There are 10 digits, 0-9. So we want to find permutations of 0^N - 9^N that have a sum that meets the criteria.

+ 3 comments I arbitrarily took the largest number that is tested to be 10^6 and my program happened to be accepted, but how would I find out the largest number that is to be tested when as far as I know, the number could be up to infinity?

+ 2 comments why i am getting wrong answer for test case 2??

+ 0 comments :-)

n=int(input()) print(sum([x for x in range(100, 1000000) if x==sum(int(z)**n for z in str(x))]))

Sort 40 Discussions, By:

Please Login in order to post a comment