w3resource

Python Challenges: Find the sum of all the numbers that can be written as the sum of fifth powers of their digits

Python Challenges - 1: Exercise-61 with Solution

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Write a Python program to find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Sample Solution:

Python Code:

def compute():
	result = sum(i for i in range(2, 1000000) if i == fifthPower_digitSum(i))
	return str(result)

def fifthPower_digitSum(n):
	return sum(int(x)**5 for x in str(n))
print("\nSum of all the numbers that can be written as the sum of fifth powers of their digits:")
print(compute())

Sample Output:

Sum of all the numbers that can be written as the sum of fifth powers of their digits:
443839

Flowchart:

Python Flowchart: Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

#Ref. https://bit.ly/2vWsRPP

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to get number of distinct terms generated by ab for 2 ≤ a ≤ 21 and 2 ≤ b ≤ 21.
Next: Write a Python program to find different ways where £2 be made using any number of coins.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.