Magic sequence problem in Picat.
A magic sequence of length n is a sequence of integers x0 . . xn-1 between
0 and n-1, such that for all i in 0 to n-1, the number i occurs exactly xi
times in the sequence. For instance, 6,2,1,0,0,0,1,0,0,0 is a magic sequence
since 0 occurs 6 times in it, 1 occurs twice, ...
Model created by Hakan Kjellerstrand, email@example.com
See also my Picat page: http://www.hakank.org/picat/
% Licenced under CC-BY-4.0 : http://creativecommons.org/licenses/by/4.0/
main => go.
foreach(N in 4..40)
if magic_sequence(N,Sequence) then
scalar_product(A, X) = Product =>
Product #= sum([S : I in 1..A.length, S #= A[I]*X[I]]).
magic_sequence(N, Sequence) =>
Sequence = new_list(N),
Sequence :: 0..N-1,
% Note: I would like to use global_cardinality/2 instead
% but didn't get it right.
foreach(I in 0..N-1) count(I,Sequence,#=,Sequence[I+1]) end,
N #= sum(Sequence),
Integers = [ I : I in 0..N-1],
N = scalar_product(Integers, Sequence),