/* Cute list in Picat. https://theweeklychallenge.org/blog/perl-weekly-challenge-191/#TASK2 """ Task 2: Cute List Submitted by: Mohammad S Anwar You are given an integer, 0 < $n <= 15. Write a script to find the number of orderings of numbers that form a cute list. With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either 1) $list[$i] is evenly divisible by $i or 2) $i is evenly divisible by $list[$i] Example Input: $n = 2 Ouput: 2 Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. Therefore we can have two list i.e. (1,2) and (2,1). @list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. """ Solutions for N=2..15 (0.2s) 2 = 2 3 = 3 4 = 8 5 = 10 6 = 36 7 = 41 8 = 132 9 = 250 10 = 700 11 = 750 12 = 4010 13 = 4237 14 = 10680 15 = 24679 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::2..15, indomain(N), A = findall(L,cute_list(N,L)), Len = A.len, println(N=Len), fail, nl. go2 :- N::15..15, indomain(N), time(A = findall(L,cute_list(N,L))), % time(A = findall(L,cute_list2(N,L))), Len = A.len, println(N=Len), % fail, nl. cute_list(N,L) :- L = new_list(N), L :: 1..N, all_different(L), foreach(I in 1..N) L[I]mod I#=0 #\/ I mod L[I] #= 0 end, % solve($[ffc,split],L). solve($[constr],L). % Alternative (Cf cute_list.pl SWI Prolog) cute_list2(N,L) :- L = new_list(N), L :: 1..N, all_different(L), check2(1,L), % solve($[ffc,split],L). solve($[constr],L). check2(_Ix,[]). check2(Ix,[H|T]) :- (H mod Ix #= 0 #\/ Ix mod H #= 0), Ix1 is Ix+1, check2(Ix1,T).