/* Euler problem 45 in SICStus Prolog """ Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ... Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ... It can be verified that T(285) = P(165) = H(143) = 40755. Find the next triangle number that is also pentagonal and hexagonal. """ 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 = [ euler45a ], run_problems(L). %% %% 0.016s %% euler45a :- T is 285+1, tri(T,TT), P is 165, pent(P,PP), H is 143, hex(H,HH), e45a(T,TT,P,PP,H,HH,X), writeln(X). e45a(_,X,_,X,_,X,X). e45a(TN,T,PN,P,HN,H,X) :- T > P, PN1 is PN+1, pent(PN1,PP), e45a(TN,T,PN1,PP,HN,H,X). e45a(TN,T,PN,P,HN,H,X) :- P > H, HN1 is HN+1, hex(HN1,HH), e45a(TN,T,PN,P,HN1,HH,X). e45a(TN,T,PN,P,HN,H,X) :- T > H, HN1 is HN+1, hex(HN1,HH), e45a(TN,T,PN,P,HN1,HH,X). e45a(TN,T,PN,P,HN,H,X) :- (T \= P ; P \= H ; T \= H), TN1 is TN+1, tri(TN,TT), e45a(TN1,TT,PN,P,HN,H,X). pent(N,P) :- P is N*(3*N-1) div 2. tri(N,T) :- T is N*(N+1) div 2. hex(N,H) :- H is N*(2*N-1).