/* Euler problem 30 in SICStus Prolog """ 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. """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my SICStus Prolog page: http://www.hakank.org/sicstus_prolog/ */ :- ensure_loaded(hakank_utils). go :- L = [ %% euler30a, % euler30b % , euler30c ], run_problems(L). %% %% 0.530s %% euler30a :- From is 10, To is 6*9^5, findall(N, (between(From,To,N), num_to_digit_list(N,Is), maplist(sum_pow5,Is,Ps), sumlist(Ps,N) ), L), sum_list(L,Sum), writeln(Sum). %% %% 0.472s %% euler30b :- From is 10, To is 6*9^5, e30b(From,To,0,S), writeln(S). e30b(N,N,S,S). e30b(N,To,S0,S) :- num_to_digit_list(N,Is), maplist(sum_pow5,Is,Ps), ( sumlist(Ps,N) -> S1 is S0 + N ; S1 is S0 ), N1 is N+1, e30b(N1,To,S1,S). %% %% For-loops: 0.460s %% euler30c :- From is 10, To is 6*9^5, /* findall(N, (between(From,To,N), num_to_digit_list(N,Is), maplist(sum_pow5,Is,Ps), sumlist(Ps,N) ), L), */ (for(N,From,To), fromto(L,Out,In,[]) do num_to_digit_list(N,Is), maplist(sum_pow5,Is,Ps), (sumlist(Ps,N) -> Out = [N|In] ; Out = In ) ), sum_list(L,Sum), writeln(Sum). sum_pow5(I,P) :- P is I^5.