/*
Magic series in Picat.
http://mathworld.wolfram.com/MagicSeries.html
"""
A set of n distinct numbers taken from the interval [1,n^2] form a magic
series if their sum is the nth magic constant
M_n=1/2n(n^2+1)
(Kraitchik 1942, p. 143). The numbers of magic series of orders
n=1, 2, ..., are 1, 2, 8, 86, 1394, ... (Sloane's A052456). The following
table gives the first few magic series of small order.
n magic series
1 {1}
2 {1,4}, {2,3}
3 {1,5,9}, {1,6,8}, {2,4,9}, {2,5,8}, {2,6,7}, {3,4,8}, {3,5,7}, {4,5,6}
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
% import util.
import cp.
main => go.
go =>
garbage_collect(100_000_000),
foreach(N in 1..7)
println(n=N),
magic_series(N,X),
println(x=X),
if N <= 7 then
All = find_all(X2, magic_series(N,X2)),
% println(all=All),
println(len=All.length)
end
end,
nl.
go2 =>
foreach(N in 1..28)
magic_series(N,X),
println(n=X)
end,
nl.
magic_series(N, X) =>
N2 = N*N,
X = new_list(N),
X :: 1..N2,
all_different(X),
increasing(X),
sum(X)*2 #= N*(N**2+1),
solve([ff,split],X).
increasing(List) =>
foreach(I in 2..List.length) List[I-1] #< List[I] end.