/*
Divisible by 1 to 9 in Picat
http://mindyourdecisions.com/blog/2016/04/10/find-the-10-digit-number-where-n-digits-are-divisible-by-n-sunday-puzzle/
"""
"
Find a 10 digit number that uses each of the digits 0 to 9 exactly once and
where the number formed by the first n digits of the number is divisible by n.
"
You should read the digits of the number from left to right. For example, in the
number abcd, you need the number a to be divisible by 1, the number ab to be
divisible by 2, the number abc to be divisible by 3, and the entire number abcd
to be divisible by 4.
"""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
foreach(Base in 2..14)
nl,
println(base=Base),
(
problem(Base, X, T) ->
println(x=X),
println(t=T),nl
;
println("No solution"),
true
)
end,
nl.
% Finds all solutions
go2 =>
foreach(Base in 2..14)
nl,
println(base=Base),
L = findall([X, T], $problem(Base, X, T)),
foreach([X2, T2] in L)
println(x=X2),
println(t=T2),
nl
end
end.
go(N) ?=>
problem(N, X, T),
println(x=X),
println(t=T),
fail.
go(_) => true.
problem(Base, X, T) =>
M = Base**(Base)-1, % largest value
N = Base, % the digits are in 1..N ,
% N is also the length of X
X = new_list(N),
X :: 0..N-1,
all_different(X),
T = new_list(N),
T :: 1..M,
foreach(I in 1..N)
XI = [X[J] : J in 1..I],
to_num(XI, Base, T[I]),
T[I] mod I #= 0
end,
Vars = T ++ X,
solve([ff,split],Vars).
to_num(List, Base, Num) =>
Len = length(List),
Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).