/* Numeric Keypad problem in Picat. From Fun with num3ers http://benvitale-funwithnum3ers.blogspot.com/2011/04/numeric-keypad.html """ Rearrange the key caps of the 1 through 9 on a numeric keypad so that no cap is on its correct key, in such a manner that each of the three rows forms a 3-digit perfect square. 7 8 9 4 5 6 1 2 3 """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 9, M = 3, % original configuration Orig = { {7, 8, 9}, {4, 5, 6}, {1, 2, 3}}, X = new_array(M,M), X :: 1..N, all_different(X.vars), % no fixpoint foreach(I in 1..M, J in 1..M) X[I,J] #!= Orig[I,J] end, % each row makes a perfect square foreach(I in 1..M) T :: 100..999, to_num(X[I],10,T), sum([Y*Y #= T : Y in 10..32]) #>= 1 end, solve(X), foreach(Row in X) println(Row) end, nl, fail, nl. go => true. % % converts a number Num to/from a list of integer List given a base Base % to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).