It is very hard to come up with Prolog answers to Project Euler. If you have any please add as a comment to problems posted here.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
gen(X,Y,[],B):- X >= Y. gen(X,Y,[Head|Tail],B):- Y>X, Head is X, X1 is X + B, gen(X1,Y,Tail,B). sum(X,Y,Sum):- gen(X,Y,List1,3), gen(X,Y,List2,5), gen(X,Y,List3,15), sumlist(List1,Sum1), sumlist(List2,Sum2), sumlist(List3,Sum3), Sum is Sum1 + Sum2 - Sum3. sumlist([],0). sumlist([H|Tail],Sum):- sumlist(Tail,Sum1), Sum is H + Sum1. |

?- sum(0,1000,X) gives the answer

Continue reading…