/*
Euler #45 in Picat.
"""
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.
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go => time(euler45).
euler45 =>
T = 285+1,
TT = tri(T),
P = 165,
PP = pent(P),
H = 143,
HH = hex(H),
while (TT != PP; PP != HH)
T := T+1,
TT := tri(T),
if TT > PP then P := P+1, PP := pent(P) end,
if PP > HH then H := H+1, HH := hex(H) end,
if TT > HH then H := H+1, HH := hex(H) end
end,
println(TT).
pent(N) = N*(3*N-1) div 2.
tri(N) = N*(N+1) div 2.
hex(N) = N*(2*N-1).