/* Smallest square that begins with n (Rosetta code) in Picat. http://rosettacode.org/wiki/Smallest_square_that_begins_with_n """ Find the smallest (decimal integer) squares that begin with n for 0 < n < 50 """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => foreach(N in 1..49) Found = false, NN = N.to_string, M = 1, while(Found == false) S = (M*M).to_string, if find(S,NN,1,_) then println(N=S), Found := M else M := M + 1 end end end, nl. go2 => println([S : N in 1..49, S = smallest_square(N)]), nl. go3 => println([S : N in 1..49, S = smallest_square2(N)]). % Recursion and string approach smallest_square(N) = Square => smallest_square(N.to_string,1,Square). smallest_square(N,S,SS) :- SS = S*S, find((SS).to_string,N,1,_). smallest_square(N,S,SS) :- smallest_square(N,S+1,SS). % Iterative smallest_square2(N) = Ret => I = 1, Found = false, while (Found == false) Square = I*I, while (Square > N) Square := Square // 10 end, if Square == N then Found := I*I end, I := I + 1 end, Ret = Found.