/*
Euler #15 in Picat.
Problem 15
"""
Starting in the top left corner of a 2×2 grid, there are 6 routes
(without backtracking) to the bottom right corner.
How many routes are there through a 20×20 grid?
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => time(go).
go => euler15.
%
% The times for these versions are barely discernible, all are
% between 0.000s and 0.004s.
%
euler15 =>
println(prodlist2(21..40) // prodlist2(2..20)).
euler15b =>
println(prod(21..40) / prod(2..20)).
euler15c =>
println(prodlist(21..40) // prodlist(2..20)).
prodlist(List) = Res =>
Res1 = 1,
foreach(I in List)
Res1 := Res1 * I
end,
Res = Res1.
%
% recursive version
prodlist2(List) = Prod =>
prodlist2_aux(List,1,Prod).
prodlist2_aux([], Prod0,Prod) =>
Prod = Prod0.
prodlist2_aux([H|T], Prod0,Prod) =>
Prod1 = H*Prod0,
prodlist2_aux(T,Prod1,Prod).