/* Euler problem 44 in SWI Prolog """ Pentagonal numbers are generated by the formula, P(n)=n(3nāˆ’1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... It can be seen that P(4) + P(7) = 22 + 70 = 92 = P(8). However, their difference, 70 āˆ’ 22 = 48, is not pentagonal. Find the pair of pentagonal numbers, P(j) and P(k), for which their sum and difference is pentagonal and D = |P(k) āˆ’ P(j)| is minimised; what is the value of D? """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my SWI Prolog page: http://www.hakank.org/swi_prolog/ */ :- use_module(library(clpfd)). :- use_module(hakank_utils). :- use_module(euler_utils). go :- L = [ euler44a %% euler44b ], run_problems(L). %% %% 3.1s %% euler44a :- M = 2500, numlist(1,M,Is), maplist(pentagonal_number,Is,Ps), list_domain_disjunction(Ps,Domain), Vars = [J,K,A,D], Vars ins Domain, J #< K, A #= J+K, D #= abs(J-K), labeling([max,down,enum],Vars), writeln(D). %% %% 148.5s %% euler44b :- M = 2500, numlist(1,M,Is), maplist(pentagonal_number,Is,Ps), findall(B, (member(J,Ps), member(K,Ps), J < K, A #= J+K, memberchk(A,Ps), B #= abs(J-K), memberchk(B,Ps) ), L), writeln(L), nl. pentagonal_number(N,P) :- P #= N*(3*N-1) div 2.