Author Topic: [Require Better Solution] Unhappy with Solution : Project Euler 119  (Read 335 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Hello EZ,

I just looked up on Project Euler again and chose a random problem page. The problem number 119 attracted my attention. I did solve it but not satisfied with the solution. To put it simple I just bruteforced it.

Here it is :-



The solution I got is correct but still would love to see some really clever solution. Most of PE problems cover some math concept. But Is there something that is clearly visible ?

Would love your feed back and some better solutions too.

EDIT: Forgot the code

Python Code

Code: [Select]
def digitsum(num):
    return sum(map(int, num))

data = []

for base in range(7, 80):
    for expo in range (2, 10):
        temp = base ** expo
        if digitsum(str(temp)) == base:
            data.append(temp)
            print("{0}^{1} = {2}".format(base, expo, temp))
print(sorted(data)[29])


EDIT:-

Python One Liner Code :-

Code: [Select]
sorted([ b**e for b in range(7, 80) for e in range(2, 10) if b == sum(map(int, str(b**e))) ])[29]
« Last Edit: March 24, 2015, 04:21:31 pm by Psycho_Coder »
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins