Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. '''Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
  2.  
  3. 1634 = 14 + 64 + 34 + 44
  4. 8208 = 84 + 24 + 04 + 84
  5. 9474 = 94 + 44 + 74 + 44
  6. As 1 = 14 is not a sum it is not included.
  7.  
  8. The sum of these numbers is 1634 + 8208 + 9474 = 19316.
  9.  
  10. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.'''
  11.  
  12. def sum_digit_power(exponent):
  13. answer = 0
  14. max_string = ''
  15. while len(max_string) < exponent:
  16. max_string += '9'
  17. while len(max_string) *(9**exponent) > int(max_string):
  18. max_string +='9'
  19. for num in range(2,int(max_string)):
  20. target =0
  21. target += num
  22. digit_power_sum = 0
  23. while digit_power_sum <= num:
  24. while num >9:
  25. digit = num % 10
  26. num = num //10
  27. digit_power_sum += digit ** exponent
  28. digit = num
  29. digit_power_sum += digit ** exponent
  30. if digit_power_sum == target:
  31. answer += target
  32. break
  33. return answer
  34. print(sum_digit_power(5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement