project_euler/30.rb
Code:
# Project Euler Problem 30: Digit Fifth Powers
# https://projecteuler.net/problem=30
# Surprisingly there are only three numbers that can be written as
# the sum of fourth powers of their digits.
# Find the sum of all the numbers that can be written as the sum of
# fifth powers of their digits.
def digit_power_sum?(n, e)
n.to_s.chars.map { |d| d.to_i ** e }.sum == n
end
def solve(e)
(2..10 ** (e + 1)).to_a.filter { |n| digit_power_sum?(n, e) }.sum
end
puts solve(4)
# puts solve(5)
Output:
19316