/* Van Eck's sequence (Rosetta code) in Picat. http://rosettacode.org/wiki/Van_Eck_sequence """ The sequence is generated by following this pseudo-code: A: The first term is zero. Repeatedly apply: If the last term is *new* to the sequence so far then: B: The next term is zero. Otherwise: C: The next term is how far back this last term occured previously. Example Using A: 0 Using B: 0 0 Using C: 0 0 1 Using B: 0 0 1 0 Using C: (zero last occurred two steps back - before the one) 0 0 1 0 2 Using B: 0 0 1 0 2 0 Using C: (two last occurred two steps back - before the zero) 0 0 1 0 2 0 2 2 Using C: (two last occurred one step back) 0 0 1 0 2 0 2 2 1 Using C: (one last appeared six steps back) 0 0 1 0 2 0 2 2 1 6 ... Task Create a function/procedure/method/subroutine/... to generate the Van Eck sequence of numbers. Use it to display here, on this page: The first ten terms of the sequence. Terms 991 - to - 1000 of the sequence. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. % {{trans|Dyalect}} % Note: Picat is 1-based go => Max = 1001, A = new_array(Max), bind_vars(A,0), foreach(N in 1..Max-2) M = N-1, Break = false, while (M >= 0, Break == false) if A[M+1] == A[N+1] then A[N+2] := N - M, Break := true end, M := M - 1 end end, println(A[1..10]), println(A[991..1000]), % print("The first ten terms of the Van Eck sequence are: \(a[0..10].ToArray())"), % print("Terms 991 to 1000 of the sequence are: \(a[991..999].ToArray())"), nl. % {{trans|FreeBasic}} go2 => Limit = 1000, A = new_array(Limit+1), bind_vars(A,0), foreach(N in 1..Limit-1) M = find_last_of(A[1..N],A[N+1]), if M > 0 then A[N+2] := N-M+1 end end, println(A[1..10]), println(A[991..1000]), nl. go3 => L = a(1000), println(L[1..10]), println(L[991..1000]), nl. % Same idea but recursive a(0) = {0}. a(1) = {0,0}. a(N) = A ++ {cond(M > 0, N-M, 0)} => A = a(N-1), M = find_last_of(slice(A,1,N-1),A.last).