/* Enigma 1181: Pandigitals in Picat. https://enigmaticcode.wordpress.com/2016/02/08/enigma-1181-pandigitals/ """ From New Scientist #2337, 6th April 2002 George has been investigating pandigital numbers, which he defines as 10-digit numbers with no leading zero, containing the digits 0-9 once each. The largest pandigital number is 9876543210. Since it ends in 0 it is clearly divisible by 2 and 5, giving quotients 4938271605 and 1975308642 – both pandigital! Inspired by this discovery, he set out to list, for each possible multiple, the largest pandigital number which is an exact multiple of another pandigital number. The solutions for 4 and 8 can be found by small-scale number juggling; the others require a more systematic approach. What is the smallest number in George’s list of largest pandigital multiples? """ ls = [9876543210,9875304162,9876543120,9876543210,9875034162,9867430125,9876543120,9876314205] min = 9867430125 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 ?=> nolog, Ls = [], foreach(Z in 2..9) puzzle(Z,X), Ls := Ls ++ [X] end, println(ls=Ls), println(min=min(Ls)), nl. go => true. puzzle(Z,X) => N = 10, XA = new_array(N), XA :: 0..9, YA = new_array(N), YA :: 0..9, Z * Y #= X, all_different(XA), to_num(XA,10,X), all_different(YA), to_num(YA,10,Y), Vars = YA ++ XA ++ {X,Y,Z}, solve($[max(X),ff,updown],Vars). % 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]).