/* Sum of next natural numbers in Picat. Identify the sequence and sum of N natural increasing numbers. This is inspired by farmer_and_cows.pi and https://oeis.org/A006003: n*(n^2 + 1) / 2 """ Write the natural numbers in groups: 1; 2,3; 4,5,6; 7,8,9,10; ... and add the groups. In other words, "sum of the next n natural numbers". ... The sequence M(n) of magic constants for n X n magic squares (numbered 1 through n^2) from n=3 begins M(n) = 15, 34, 65, 111, 175, 260, .. """ The number of solutions (without the "identify constraint") for n is n*n-n. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 9, println(n=N), sum_of_next_natural_numbers(N, X, Z), println(x=X), println(z=Z), nl, fail, nl. go2 => member(N, 1..10), println(n=N), sum_of_next_natural_numbers(N, X, Z), println(x=X), println(z=Z), nl, fail, nl. sum_of_next_natural_numbers(N, X, Z) => X = new_list(N), X :: 1..N*N, Z #= sum(X), all_different(X), increasing(X), X[N] - X[1] #= N-1, % "identify constraint" % identify the sequence for the "farmer and cows" solution Z #= N*(N*N + 1) div 2, solve(X ++ [Z]).