Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m