/* Enigma 1577: Happy New Year in Picat. From https://enigmaticcode.wordpress.com/2012/02/09/enigma-1577-happy-new-year/#comment-5842 """ From New Scientist #2742, 9th January 2010 [link] I have written down three 3-digit numbers which between them use nine different digits. One of the numbers is a perfect square and another is a triangular number. The sum of the three numbers is 2010. What, in ascending order, are the three numbers? """ This is a port of geoffrounce's MiniZinc model This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. go ?=> L = [A,B,C,D,E,F,G,H,I], L :: 0..9, all_different(L), ABC :: 123..987, ABC #= 100*A + 10*B + C, DEF :: 123..987, DEF #= 100*D + 10*E + F, GHI :: 123..987, GHI #= 100*G + 10*H + I, All = [ABC,DEF,GHI], sum(All) #= 2010, % ABC + DEF+ GHI #= 2010, increasing(All), % ABC #< DEF, DEF #< GHI, Sq3 = [ X**2 : X in 10..31], Tri3 = [ N * (N+1) div 2 : N in 14..44], sum([X :: Sq3 #<=> 1 : X in All]) #= 1, sum([X :: Tri3 #<=> 1 : X in All]) #= 1, solve($[ff,split],L), println([ABC,DEF,GHI]), fail, nl. go => true. go2 => L = [A,B,C,D,E,F,G,H,I], L :: 0..9, all_different(L), ABC :: 123..987, ABC #= 100*A + 10*B + C, DEF :: 123..987, DEF #= 100*D + 10*E + F, GHI :: 123..987, GHI #= 100*G + 10*H + I, ABC + DEF + GHI #= 2010, All = [ABC,DEF,GHI], increasing(All), Sq3 = [ X**2 : X in 10..31], Tri3 = [ N * (N+1) div 2 : N in 14..44], TriIx :: 0..3, SqIx :: 0..3, (ABC :: Sq3 , SqIx #= 1 ; DEF :: Sq3 , SqIx #= 2 ; GHI :: Sq3 , SqIx #= 3), (ABC :: Tri3 , TriIx #= 1 ; DEF :: Tri3 , TriIx #= 2 ; GHI :: Tri3 , TriIx #= 3), TriIx #> 0, SqIx #> 0, TriIx #!= SqIx, solve($[ff,split],L), println([ABC,DEF,GHI]), fail, nl. go3 => L = [A,B,C,D,E,F,G,H,I], L :: 0..9, all_different(L), % Form the three numbers in ascending order ABC :: 123..987, ABC #= 100*A + 10*B + C, DEF :: 123..987, DEF #= 100*D + 10*E + F, GHI :: 123..987, GHI #= 100*G + 10*H + I, All = [ABC,DEF,GHI], increasing(All), ABC + DEF + GHI #= 2010, solve($[ff,split],L), CC = 0, Sq3 = [ X**2 : X in 10..31], Tri3 = [ N * (N+1) div 2 : N in 14..44], if ABC :: Sq3 then CC := CC + 1 end, if DEF :: Sq3 then CC := CC + 1 end, if GHI :: Sq3 then CC := CC + 1 end, if ABC :: Tri3 then CC := CC + 1 end, if DEF :: Tri3 then CC := CC + 1 end, if GHI :: Tri3 then CC := CC + 1end, CC == 2, println([ABC,DEF,GHI]), fail, nl. tri(X,B) => % Tri3 = [ N * (N+1) div 2 : N in 14..44], % X :: Tri3, B :: 0..1, N :: 14..44, X #= N * (N+1) div 2 #<=> B #= 1. perfect_square(X,B) => B :: 0..1, N :: 10..31, X #= N*N #<=> B #= 1. perfect_square2(X,B) => println($perfect_square(X)), Z :: 0..999, B :: 0..1, B #=1 #<=> Z*Z #= X. inx(X,List,B) => println($inx(X,List,B)), Ix :: 1..List.len, element(Ix,List,X), B #>= 0.