/* Digits of the Square puzzle in Picat. http://en.wikibooks.org/wiki/Puzzles/Arithmetical_puzzles/Digits_of_the_Square """ There is one four-digit whole number n, such that the last four digits of n^2 are in fact the original number n. What is it? """ 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 :: 1000..9999, NA = new_list(4), NA :: 0..9, NSquared :: 1000000..99980001, NSquaredA = new_list(8), NSquaredA :: 0..9, % doing it the hard way ... to_num(NA, 10, N), to_num(NSquaredA, 10, NSquared), foreach(I in 5..8) NA[I-4] #= NSquaredA[I] end, N*N #= NSquared, Vars = NA ++ NSquaredA ++ [N,NSquared], solve(Vars), println(n=N), println(nSquared=NSquared), println(na=NA), println(nSquaredA=NSquaredA), 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]).