/* Special 6 digit number in Picat. From Presh Talwalkar (Mind Your Decisions) https://www.youtube.com/watch?v=KXOjtmNUSH0&feature=youtu.be """ A 6 digit number abcdef has the following property. Make a table with 6 rows, where row 2 is the number abcdef multiplied by 2, and row i is the number abcdef multiplied by i for values i = 3, 4, 5, 6. (If the first row was 123456, the second row would be 123456 x 2 = 246912). For this number abcdef, the table is a Latin square–each row has 6 different digits with no repeats, and each column also has those 6 different digits with no repeats. Can you figure out a value of abcdef? """ Solution: y: [142857, 285714, 428571, 571428, 714285, 857142] 1 4 2 8 5 7 2 8 5 7 1 4 4 2 8 5 7 1 5 7 1 4 2 8 7 1 4 2 8 5 8 5 7 1 4 2 ---------- ========== This is the "magic" 1/7 series: Picat> println([to_fstring("\n%0.6f", I*1/7) : I in 1..6]) [ 0.142857, 0.285714, 0.428571, 0.571429, 0.714286, 0.857143] Number of solutions for different n: n # solutions ---------------- (0 1) 1 10 2 37 3 84 4 96 5 21 6 1 7 0 The sequence is not found in OEIS: [10,37,84,96,21,1] . 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 = 6, special_number(N, X, Y), println(x=X), println(y=Y), nl, fail, % check unicity nl. go => true. go2 => Lens = [], foreach(N in 0..10) println(n=N), All = findall(Y,special_number(N, X, Y)), println(All), println(len=All.len), Lens := Lens ++ [All.len], nl, end, println(lens=Lens), nl. special_number(N, X,Y) => X = new_array(N,N), % the matrix X :: 0..9, Y = new_array(N), % the numbers Y :: 0..10**N-1, foreach(I in 1..N) % convert row to a number to_num([X[I,J] : J in 1..N],10,Y[I]), Y[I] #= I*Y[1], % latin square all_different([X[I,J] : J in 1..N]), all_different([X[J,I] : J in 1..N]) end, Vars = X.vars() ++ Y, solve($[],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]).