% https://open.kattis.com/problems/theplank % 1s % 1.8 Easy % Some investigation in Picat import cp. main :- % time(test_cp), Tri = [a(N) : N in 1..24], println(tri=Tri), nl. /* This takes 1.757s 1 = 1 2 = 2 3 = 4 4 = 7 5 = 13 6 = 24 7 = 44 8 = 81 9 = 149 10 = 274 11 = 504 12 = 927 13 = 1705 14 = 3136 15 = 5768 16 = 10609 17 = 19513 18 = 35890 19 = 66012 20 = 121415 21 = 223317 22 = 410744 23 = 755476 24 = 1389537 */ test_cp:- member(P,1..24), println(P=findall(A,( member(N,1..P),X=new_list(N),X::1..3,sum(X)#=P,solve_all(X).len=A)).sum), fail, nl. /* http://oeis.org/A000073 "Tribonacci numbers: a(n) = a(n-1) + a(n-2) + a(n-3) for n >= 3 with a(0) = a(1) = 0 and a(2) = 1." Note that we fiddle with the start values: it's [1,1,2] instead of [0,0,1]: This takes 0.033s tri = [1,2,4,7,13,24,44,81,149,274,504,927,1705,3136,5768,10609,19513,35890,66012,121415,223317,410744,755476,1389537] picat -log the_plank.pi 0,01s user 0,02s system 98% cpu 0,033 total */ table a(0) = 1. a(1) = 1. a(2) = 2. a(N) = a(N-1) + a(N-2) + a(N-3).