/*
Euler #30 in Picat.
Problem 30
"""
Surprisingly there are only three numbers that can be written
as the sum of fourth powers of their digits:
1634 = 1^(4) + 6^(4) + 3^(4) + 4^(4)
8208 = 8^(4) + 2^(4) + 0^(4) + 8^(4)
9474 = 9^(4) + 4^(4) + 7^(4) + 4^(4)
As 1 = 1^(4) is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of
fifth powers of their digits.
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go => time(euler30). % , time(euler30b).
% 0.584s
euler30 =>
println(sum([N : N in 10..6*9**5,
N == sum([I**5 : I in [J.to_integer() : J in N.to_string()]])])).
% 0.634s
euler30b =>
T = 0,
M = 5,
foreach(N in 10..6*9**5)
if N == sum([I**M : I in [J.to_integer() : J in N.to_string() ]]) then
T := T + N
end
end,
println(T).
% Caching .to_integer() it's slower: 0.674s
euler30c =>
println(sum([N : N in 10..6*9**5,
N == sum([I**5 : I in [toint(J) : J in N.to_string()]])])).
table
toint(J) = J.to_integer().