top of page

Project Euler #30: Finding Numbers Equal to the Sum of Fifth Powers of Their Digits

Dec 16

1 min read

0

1

0

ree


Tackled another Project Euler challenge - this time finding all numbers that can be written as the sum of the fifth powers of their digits.

The problem excludes 1 (as per the problem statement), and asks: which numbers equal the sum of their digits raised to the fifth power?

For example: 4150 = 4⁵ + 1⁵ + 5⁵ + 0⁵ = 1024 + 1 + 3125 + 0 = 4150

My Python solution iterates through numbers up to 9,999,999, converts each to individual digits, calculates the sum of fifth powers, and checks for equality:

import math
list_x = []

for i in range(0, 9999999):
    n = list(str(i))
    if len(str(i)) == 6:
        if int(n[0]) ** 5 + int(n[1]) ** 5 + int(n[2]) ** 5 + int(n[3]) ** 5 + int(n[4]) ** 5 + int(n[5]) ** 5 == i:
            list_x.append(i)
    if len(str(i)) == 5:
        if int(n[0]) ** 5 + int(n[1]) ** 5 + int(n[2]) ** 5 + int(n[3]) ** 5 + int(n[4]) ** 5 == i:
            list_x.append(i)
    if len(str(i)) == 4:
        if int(n[0]) ** 5 + int(n[1]) ** 5 + int(n[2]) ** 5 + int(n[3]) ** 5 == i:
            list_x.append(i)
    if len(str(i)) == 3:
        if int(n[0]) ** 5 + int(n[1]) ** 5 + int(n[2]) ** 5 == i:
            list_x.append(i)
    if len(str(i)) == 2:
        if int(n[0]) ** 5 + int(n[1]) ** 5 == i:
            list_x.append(i)
    if len(str(i)) == 1 and i != 1 and i != 0:
        if int(n[0]) ** 5 == i:
            list_x.append(i)

print(list_x)
print(sum(list_x))

The approach handles different digit lengths separately to avoid index errors and efficiently checks each case.

Final answer: 443,839

#ProjectEuler #Python #DataEngineering #ProblemSolving

Dec 16

1 min read

0

1

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page