princeofsudan

ProjectEuler#30 Digit fifth powers

Mar 20th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. #Surprisingly there are only three numbers that can be written as the sum of
  2. #fourth powers of their digits:
  3.  
  4. #    1634 = 1**4 + 6**4 + 3**4 + 4**4
  5. #    8208 = 8**4 + 2**4 + 0**4 + 8**4
  6. #    9474 = 9**4 + 4**4 + 7**4 + 4**4
  7.  
  8. #As 1 = 1**4 is not a sum it is not included.
  9.  
  10. #The sum of these numbers is 1634 + 8208 + 9474 = 19316.
  11.  
  12. #Find the sum of all the numbers that can be written as the sum of fifth
  13. #powers of their digits.
  14.  
  15. #A Perfect Digital Invariant (PDI) is an integer, the sum of whose digits,
  16. #each raised to the same integral power, equals the number itself.
  17.  
  18. def dSumP5(n):
  19.     acc = 0
  20.     for i in str(n):
  21.         acc += int(i)**5
  22.     return acc
  23.  
  24. limit = 6*(9**5)        
  25. PDI = []
  26. acc = 0
  27. for i in range(2,limit+1):
  28.     if i == dSumP5(i):
  29.         PDI.append(i)
  30.         acc += i
  31.  
  32. print PDI, acc
Advertisement
Add Comment
Please, Sign In to add comment