Jump to content

Van Eck sequence: Difference between revisions

m
(Add COBOL)
m (→‎{{header|Uiua}}: Reformat)
 
(45 intermediate revisions by 23 users not shown)
Line 7:
B: The next term is zero.
Otherwise:
C: The next term is how far back this last term occured previouselypreviously.
</pre>
 
Line 20:
Using B:
:<code>0 0 1 0</code>
Using C: (zero last occuredoccurred two steps back - before the one)
:<code>0 0 1 0 2</code>
Using B:
:<code>0 0 1 0 2 0</code>
Using C: (two last occuredoccurred two steps back - before the zero)
:<code>0 0 1 0 2 0 2 2</code>
Using C: (two last occuredoccurred one step back)
:<code>0 0 1 0 2 0 2 2 1</code>
Using C: (one last appeared six steps back)
:<code>0 0 1 0 2 0 2 2 1 6</code>
...
 
 
;Task:
# Create a function/proceedureprocedure/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.
 
 
;References:
* [https://www.youtube.com/watch?v=etMJxB-igrc Don't Know (the Van Eck Sequence) - Numberphile video].
* [[wp:Van_Eck%27s_sequence|Wikipedia Article: Van Eck's Sequence]].
* [[oeisOEIS:A181391| OEIS sequence: A181391]].
<br><br>
 
=={{header|11l}}==
{{trans|Python: List based}}
 
<langsyntaxhighlight lang="11l">F van_eck(c)
[Int] r
V n = 0
Line 63 ⟶ 66:
 
print(‘Van Eck: first 10 terms: ’van_eck(10))
print(‘Van Eck: terms 991 - 1000: ’van_eck(1000)[(len)-10..])</langsyntaxhighlight>
 
{{out}}
Line 72 ⟶ 75:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm"> org 100h
lxi h,ecks ; Zero out 2000 bytes
lxi b,0
Line 181 ⟶ 184:
db '.....'
buf: db ' $'
ecks: equ $</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 187 ⟶ 190:
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm">LIMIT: equ 1000
cpu 8086
org 100h
Line 248 ⟶ 251:
buf: db ' $'
section .bss
eck: resw LIMIT</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 255 ⟶ 258:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program vanEckSerie64.s */
Line 356 ⟶ 359:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN vaneck.sequence length:
PUT {[1]: 0} IN seq
WHILE #seq < length:
PUT #seq-1 IN i
WHILE i>0 AND seq[i]<>seq[#seq]: PUT i-1 IN i
SELECT:
i=0: PUT 0 IN seq[#seq+1]
ELSE: PUT #seq-i IN seq[#seq+1]
RETURN seq
 
PUT vaneck.sequence 1000 IN eck
FOR i IN {1..10}: WRITE eck[i]>>4
WRITE /
FOR i IN {991..1000}: WRITE eck[i]>>4
WRITE /</syntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC LastPos(INT ARRAY a INT count,value)
INT pos
pos=count-1
WHILE pos>=0 AND a(pos)#value
DO
pos==-1
OD
RETURN (pos)
 
PROC Main()
DEFINE MAX="1000"
INT ARRAY seq(MAX)
INT i,pos
 
seq(0)=0
FOR i=1 TO MAX-1
DO
pos=LastPos(seq,i-1,seq(i-1))
IF pos>=0 THEN
seq(i)=i-1-pos
ELSE
seq(i)=0
FI
OD
 
PrintE("Van Eck first 10 terms:")
FOR i=0 TO 9
DO
PrintI(seq(i)) Put(32)
OD
PutE() PutE()
 
PrintE("Van Eck terms 991-1000:")
FOR i=990 TO 999
DO
PrintI(seq(i)) Put(32)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Van_Eck_sequence.png Screenshot from Atari 8-bit computer]
<pre>
Van Eck first 10 terms:
0 0 1 0 2 0 2 2 1 6
 
Van Eck terms 991-1000:
4 7 30 25 67 225 488 0 10 136
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Van_Eck_Sequence is
Line 397 ⟶ 469:
Show (First => 1, Last => 10);
Show (First => 991, Last => 1_000);
end Van_Eck_Sequence;</langsyntaxhighlight>
 
{{out}}
Line 404 ⟶ 476:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find elements of the Van Eck Sequence - first term is 0, following #
# terms are 0 if the previous was the first appearance of the element #
# or how far back in the sequence the last element appeared #
Line 430 ⟶ 502:
FOR i FROM UPB seq - 9 TO UPB seq DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 438 ⟶ 510:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algol">begin
integer array eck[1:1000];
integer i, j;
Line 460 ⟶ 532:
writeon(eck[i]);
 
end</langsyntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
Line 467 ⟶ 539:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="algolw">begin % find elements of the Van Eck Sequence - first term is 0, following %
% terms are 0 if the previous was the first appearance of the element %
% or how far back in the sequence the last element appeared %
Line 499 ⟶ 571:
write()
end
end.</langsyntaxhighlight>
{{out}}
<pre>
0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
decimales '0'
dimensionar(2) matriz de ceros (vaneck)
iterar para (i=2, #(i<=1000), ++i )
matriz.buscar en reversa(#(vaneck[i]),#(vaneck[1:i-1]))
---retener--- no es cero?, entonces{ menos'i' por'-1' }
meter en 'vaneck'
siguiente
 
imprimir ( "Van Eck: first 10 terms : ",#(vaneck[1:10]), NL,\
"Van Eck: terms 991 - 1000: ", #(vaneck[991:1000]), NL)
 
terminar
</syntaxhighlight>
{{out}}
<pre>
Van Eck: first 10 terms : 0,0,1,0,2,0,2,2,1,6
Van Eck: terms 991 - 1000: 4,7,30,25,67,225,488,0,10,136
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">(10∘↑,[.5]¯10∘↑)(⊢,(⊃∘⌽∊¯1∘↓)∧(1↓⌽)⍳⊃∘⌽)⍣999⊢,0</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 516 ⟶ 614:
===Functional===
AppleScript is not the tool for the job, but here is a quick assembly from ready-made parts:
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use scripting additions
 
Line 688 ⟶ 786:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>First 10 terms:
Line 699 ⟶ 797:
On the contrary, it's right up AppleScript's street.
 
<langsyntaxhighlight lang="applescript">on vanEckSequence(limit)
script o
property sequence : {}
Line 726 ⟶ 824:
 
-- Task code:
tell vanEckSequence(1000) to return {items 1 thru 10, items 991 thru 1000}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{0, 0, 1, 0, 2, 0, 2, 2, 1, 6}, {4, 7, 30, 25, 67, 225, 488, 0, 10, 136}}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program vanEckSerie.s */
Line 836 ⟶ 934:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">Max: 1000
a: array.of: Max 0
 
loop 0..Max-2 'n [
if 0 =< n-1 [
loop (n-1)..0 'm [
if a\[m]=a\[n] [
a\[n+1]: n-m
break
]
]
]
]
 
print "The first ten terms of the Van Eck sequence are:"
print first.n:10 a
 
print ""
print "Terms 991 to 1000 of the sequence are:"
print last.n:10 a</syntaxhighlight>
 
{{out}}
 
<pre>The first ten terms of the Van Eck sequence are:
0 0 1 0 2 0 2 2 1 6
 
Terms 991 to 1000 of the sequence are:
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f VAN_ECK_SEQUENCE.AWK
# converted from Go
Line 866 ⟶ 995:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 874 ⟶ 1,003:
 
=={{header|BASIC}}==
{{works with|QBasic}}
<lang basic>10 DEFINT A-Z
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM E(1000)
30 FOR I=0 TO 999
Line 884 ⟶ 1,014:
90 FOR I=0 TO 9: PRINT E(I);: NEXT
95 PRINT
100 FOR I=990 TO 999: PRINT E(I);: NEXT</langsyntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">limite = 1001
dim a(limite) fill 0
 
for n = 0 to limite-1
for m = n-1 to 0 step -1
if a[m] = a[n] then
a[n+1] = n-m
exit for
end if
next m
next n
 
print "Secuencia de Van Eck:" & Chr(10)
print "Primeros 10 terminos: ";
for i = 0 to 9
print a[i]; " ";
next i
print chr(10) & "Terminos 991 al 1000: ";
for i = 990 to 999
print a[i]; " ";
next i</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 cls
110 limite = 1000
120 dim a(limite)
130 '
140 for n = 0 to limite-1
150 for m = n-1 to 0 step -1
160 if a(m) = a(n) then
170 a(n+1) = n-m
180 exit for
190 endif
200 next m
210 next n
220 '
230 print "Secuencia de Van Eck:";chr$(10)
240 print "Primeros 10 terminos: ";
250 for i = 0 to 9
260 print a(i);
270 next i
280 print chr$(10);"Terminos 991 al 1000: ";
290 for i = 990 to 999
300 print a(i);
310 next i
320 end</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">define limit = 1000
 
dim list[limit]
 
print "calculating van eck sequence..."
 
for n = 0 to limit - 1
 
for m = n - 1 to 0 step -1
 
if list[m] = list[n] then
 
let c = n + 1
let list[c] = n - m
 
break m
 
endif
 
wait
 
next m
 
next n
 
print "first 10 terms: "
 
for i = 0 to 9
 
print list[i]
 
next i
 
print "terms 991 to 1000: "
 
for i = 990 to 999
 
print list[i]
 
next i</syntaxhighlight>
{{out| Output}}<pre>calculating van eck sequence...
first 10 terms: 0 0 1 0 2 0 2 2 1 6
terms 991 to 1000: 4 7 30 25 67 225 488 0 10 136 </pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">Const limite = 1000
 
Dim As Integer a(limite), n, m, i
 
For n = 0 To limite-1
For m = n-1 To 0 Step -1
If a(m) = a(n) Then a(n+1) = n-m: Exit For
Next m
Next n
 
Print "Secuencia de Van Eck:" &Chr(10)
Print "Primeros 10 terminos: ";
For i = 0 To 9
Print a(i) &" ";
Next i
Print Chr(10) & "Terminos 991 al 1000: ";
For i = 990 To 999
Print a(i) &" ";
Next i
End</syntaxhighlight>
{{out}}
<pre>Secuencia de Van Eck:
 
Primeros 10 terminos: 0 0 1 0 2 0 2 2 1 6
Terminos 991 al 1000: 4 7 30 25 67 225 488 0 10 136</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 L = 1000
120 DIM A(L)
130 FOR N = 0 TO L
140 LET A(N) = 0
150 NEXT N
160 FOR N = 0 TO L-1
170 FOR M = N-1 TO 0 STEP -1
180 IF A(M) = A(N) THEN A(N+1) = N - M : GOTO 200
190 NEXT M
200 NEXT N
210 PRINT "Secuencia de Van Eck:"; CHR$(10)
220 PRINT "Primeros 10 terminos: ";
230 FOR I = 0 TO 9
240 PRINT A(I);
250 NEXT I
260 PRINT CHR$(10); "Terminos 991 al 1000: ";
270 FOR I = 990 TO 999
280 PRINT A(I);
290 NEXT I
300 END</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">If OpenConsole()
Define.i n, m, i, limite
limite = 1001
Dim a.i(limite)
For n = 0 To limite-1
For m = n-1 To 0 Step -1
If a(m) = a(n)
a(n+1) = n-m
Break
EndIf
Next m
Next n
PrintN("Secuencia de Van Eck:" + #CRLF$)
Print("Primeros 10 terminos: ")
For i = 0 To 9
Print(Str(a(i)) + " ")
Next i
Print(#CRLF$ + "Terminos 991 al 1000: ")
For i = 990 To 999
Print(Str(a(i)) + " ")
Next i
PrintN(#CRLF$ + "Press ENTER to exit" + #CRLF$ ): Input()
CloseConsole()
EndIf</syntaxhighlight>
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">CONST limite = 1000
DIM a(limite)
 
FOR n = 0 TO limite - 1
FOR m = n - 1 TO 0 STEP -1
IF a(m) = a(n) THEN
a(n + 1) = n - m
EXIT FOR
END IF
NEXT m
NEXT n
 
PRINT "Secuencia de Van Eck:"; CHR$(10)
PRINT "Primeros 10 terminos: ";
FOR i = 0 TO 9
PRINT a(i);
NEXT i
PRINT CHR$(10); "Terminos 991 al 1000: ";
FOR i = 990 TO 999
PRINT a(i);
NEXT i
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{trans|FreeBASIC}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Van Eck sequence"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
l = 1000
DIM a[l]
FOR n = 0 TO l-1
FOR m = n-1 TO 0 STEP -1
IF a[m] = a[n] THEN
a[n+1] = n-m
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "Secuencia de Van Eck:"; CHR$(10)
PRINT "Primeros 10 terminos: ";
FOR i = 0 TO 9
PRINT a[i]; " ";
NEXT i
PRINT CHR$(10); "Terminos 991 al 1000: ";
FOR i = 990 TO 999
PRINT a[i]; " ";
NEXT i
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">limite = 1001
dim a(limite)
 
for n = 0 to limite-1
for m = n-1 to 0 step -1
if a(m) = a(n) then
a(n+1) = n-m
break
fi
next m
next n
 
print "Secuencia de Van Eck: \n"
print "Primeros 10 terminos: ";
for i = 0 to 9
print a(i), " ";
next i
print "\nTerminos 991 al 1000: ";
for i = 990 to 999
print a(i), " ";
next i
print</syntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let start() be
Line 907 ⟶ 1,302:
for i = 990 to 999 do writed(eck!i, 4)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">EckStep ← ⊢∾(⊑⊑∘⌽∊1↓⌽)◶(0˙)‿((⊑1+⊑⊐˜ 1↓⊢)⌽)
Eck ← {(EckStep⍟(𝕩-1))⥊0}
(Eck 10)≍990↓Eck 1000</syntaxhighlight>
{{out}}
<pre>┌─
╵ 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
┘</pre>
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
 
Line 934 ⟶ 1,338:
 
return 0;
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 946 ⟶ 1,350:
=={{header|C#|CSharp}}==
{{trans|C}}
<langsyntaxhighlight lang="csharp">using System.Linq; class Program { static void Main() {
int a, b, c, d, e, f, g; int[] h = new int[g = 1000];
for (a = 0, b = 1, c = 2; c < g; a = b, b = c++)
Line 952 ⟶ 1,356:
if (f == h[d--]) { h[c] = e; break; }
void sho(int i) { System.Console.WriteLine(string.Join(" ",
h.Skip(i).Take(10))); } sho(0); sho(990); } }</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 959 ⟶ 1,363:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
 
Line 992 ⟶ 1,396:
std::cout << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,003 ⟶ 1,407:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn van-eck
([] (van-eck 0 0 {}))
([val n seen]
Line 1,014 ⟶ 1,418:
 
(println "First 10 terms:" (take 10 (van-eck)))
(println "Terms 991 to 1000 terms:" (take 10 (drop 990 (van-eck))))</langsyntaxhighlight>
 
{{out}}
<pre>First 10 terms: (0 0 1 0 2 0 2 2 1 6)
Terms 991 to 1000 terms: (4 7 30 25 67 225 488 0 10 136)</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Generate the first N elements of the Van Eck sequence
eck = proc (n: int) returns (array[int])
ai = array[int]
e: ai := ai$fill(0, n, 0)
for i: int in int$from_to(ai$low(e), ai$high(e)-1) do
for j: int in int$from_to_by(i-1, ai$low(e), -1) do
if e[i] = e[j] then
e[i+1] := i-j
break
end
end
end
return(e)
end eck
 
% Show 0..9 and 990..999
start_up = proc ()
po: stream := stream$primary_output()
e: array[int] := eck(1000)
stream$puts(po, " 0 - 9: ")
for i: int in int$from_to(0,9) do
stream$putright(po, int$unparse(e[i]), 4)
end
stream$puts(po, "\n990 - 999: ")
for i: int in int$from_to(990,999) do
stream$putright(po, int$unparse(e[i]), 4)
end
stream$putl(po, "")
end start_up</syntaxhighlight>
{{out}}
<pre> 0 - 9: 0 0 1 0 2 0 2 2 1 6
990 - 999: 4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. VAN-ECK.
Line 1,066 ⟶ 1,506:
COMPUTE ECK(I) = (I - J) - 1.
DONE. EXIT.</langsyntaxhighlight>
{{out}}
<pre>ECK( 1) = 0
Line 1,088 ⟶ 1,528:
ECK( 999) = 10
ECK(1000) = 136</pre>
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 DIM eck#(0:1000)
0020 FOR i#:=1 TO 999 DO
0030 j#:=i#-1
0040 WHILE j#>0 AND eck#(i#)<>eck#(j#) DO j#:-1
0050 IF j#<>0 THEN eck#(i#+1):=i#-j#
0060 ENDFOR i#
0070 ZONE 5
0080 FOR i#:=1 TO 10 DO PRINT eck#(i#),
0090 PRINT
0100 FOR i#:=991 TO 1000 DO PRINT eck#(i#),
0110 PRINT
0120 END</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
;;Tested using CLISP
 
Line 1,110 ⟶ 1,567:
(format t "The first 10 elements are ~a~%" (VanEck 9))
(format t "The 990-1000th elements are ~a~%" (nthcdr 990 (VanEck 999)))
</syntaxhighlight>
</lang>
{{out}}
<pre>The first 10 elements are (0 0 1 0 2 0 2 2 1 6)
The 990-1000th elements are (4 7 30 25 67 225 488 0 10 136)
</pre>
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun van-eck-nm-sequence (n m)
(loop with ac repeat m
Line 1,124 ⟶ 1,581:
(format t "The first 10 elements are: ~{~a ~}~%" (van-eck-nm-sequence 1 10))
(format t "The 991-1000th elements are: ~{~a ~}" (van-eck-nm-sequence 991 1000))
</syntaxhighlight>
</lang>
{{out}}
<pre>The first 10 elements are: 0 0 1 0 2 0 2 2 1 6
Line 1,130 ⟶ 1,587:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub print_list(ptr: [uint16], n: uint8) is
Line 1,162 ⟶ 1,619:
 
print_list(&eck[0], 10);
print_list(&eck[LIMIT-10], 10);</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 1,169 ⟶ 1,626:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.stdio;
 
void vanEck(int firstIndex, int lastIndex) {
Line 1,193 ⟶ 1,650:
writeln("Terms 991 to 1000 of Van Eck's sequence:");
vanEck(991, 1000);
}</langsyntaxhighlight>
{{out}}
<pre>First 10 terms of Van Eck's sequence:
Line 1,221 ⟶ 1,678:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Van_Eck_sequence#Pascal Pascal].
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Fill array with Van Eck sequence */
proc nonrec make_eck([*] word eck) void:
int i, j, max;
max := dim(eck,1)-1;
for i from 0 upto max do eck[i] := 0 od;
for i from 0 upto max-1 do
j := i - 1;
while j >= 0 and eck[i] ~= eck[j] do
j := j - 1
od;
if j >= 0 then
eck[i+1] := i - j
fi
od
corp
 
/* Print eck[0..9] and eck[990..999] */
proc nonrec main() void:
word i;
[1000] word eck;
make_eck(eck);
for i from 0 upto 9 do write(eck[i]:4) od;
writeln();
for i from 990 upto 999 do write(eck[i]:4) od;
writeln()
corp</syntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|Dyalect}}==
Line 1,226 ⟶ 1,716:
{{trans|Go}}
 
<langsyntaxhighlight lang="dyalect">let max = 1000
var a = Array.emptyEmpty(max, 0)
for n in 0..(max-2) {
var m = n - 1
Line 1,238 ⟶ 1,728:
}
}
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())")</langsyntaxhighlight>
 
{{out}}
 
<pre>The first ten terms of the Van Eck sequence are: {[0, 0, 1, 0, 2, 0, 2, 2, 1, 6}]
Terms 991 to 1000 of the sequence are: {[7, 30, 25, 67, 225, 488, 0, 10, 136}]</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
len arr[] 1000
for n to 1000 - 1
for m = n - 1 downto 1
if arr[m] = arr[n]
arr[n + 1] = n - m
break 1
.
.
.
for i to 10
write arr[i] & " "
.
print ""
for i = 991 to 1000
write arr[i] & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
===The function===
<langsyntaxhighlight lang="fsharp">
// Generate Van Eck's Sequence. Nigel Galloway: June 19th., 2019
let ecK()=let n=System.Collections.Generic.Dictionary<int,int>()
Seq.unfold(fun (g,e)->Some(g,((if n.ContainsKey g then let i=n.[g] in n.[g]<-e;e-i else n.[g]<-e;0),e+1)))(0,0)
</syntaxhighlight>
</lang>
 
===The Task===
;First 50
<langsyntaxhighlight lang="fsharp">
ecK() |> Seq.take 50 |> Seq.iter(printf "%d "); printfn "";;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,264 ⟶ 1,774:
</pre>
;50 from 991
<langsyntaxhighlight lang="fsharp">
ecK() |> Seq.skip 990 |> Seq.take 50|> Seq.iter(printf "%d "); printfn "";;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,277 ⟶ 1,787:
</pre>
===Alternative recursive procedure===
<langsyntaxhighlight lang="fsharp">
open System.Collections.Generic
let VanEck() =
Line 1,299 ⟶ 1,809:
VanEck() |> Seq.take 10 |> Seq.map (sprintf "%i") |> String.concat " " |> printfn "The first ten terms of the sequence : %s"
VanEck() |> Seq.skip 990 |> Seq.take 10 |> Seq.map (sprintf "%i") |> String.concat " " |> printfn "Terms 991 - to - 1000 of the sequence : %s"
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs fry kernel make math namespaces prettyprint
sequences ;
 
Line 1,313 ⟶ 1,823:
] { } make ;
 
1000 van-eck 10 [ head ] [ tail* ] 2bi [ . ] bi@</langsyntaxhighlight>
{{out}}
<pre>
Line 1,321 ⟶ 1,831:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran"> program VanEck
implicit none
integer eck(1000), i, j
Line 1,344 ⟶ 1,854:
write (*,*)
end program</langsyntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|FreeBASICFōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Van_Eck_sequence}}
<lang freebasic>
Const limite = 1000
 
'''Solution'''
Dim As Integer a(limite), n, m, i
 
[[File:Fōrmulæ - Van Eck sequence 01.png]]
For n = 0 To limite-1
For m = n-1 To 0 Step -1
If a(m) = a(n) Then a(n+1) = n-m: Exit For
Next m
Next n
 
'''Test case'''
Print "Secuencia de Van Eck:" &Chr(10)
Print "Primeros 10 terminos: ";
For i = 0 To 9
Print a(i) &" ";
Next i
Print Chr(10) & "Terminos 991 al 1000: ";
For i = 990 To 999
Print a(i) &" ";
Next i
End
</lang>
{{out}}
<pre>
Secuencia de Van Eck:
 
[[File:Fōrmulæ - Van Eck sequence 02.png]]
Primeros 10 terminos: 0 0 1 0 2 0 2 2 1 6
Terminos 991 al 1000: 4 7 30 25 67 225 488 0 10 136
</pre>
 
=={{header|Fōrmulæ}}==
 
In [https://wiki.formulae.org/Van_Eck_sequence this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
[[File:Fōrmulæ - Van Eck sequence 03.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,408 ⟶ 1,892:
fmt.Println("\nTerms 991 to 1000 of the sequence are:")
fmt.Println(a[990:])
}</langsyntaxhighlight>
 
{{out}}
Line 1,420 ⟶ 1,904:
 
Alternatively, using a map to store the latest index of terms previously seen (output as before):
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,438 ⟶ 1,922:
fmt.Println("\nTerms 991 to 1000 of the sequence are:")
fmt.Println(a[990:])
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (elemIndex)
import Data.Maybe (maybe)
 
Line 1,453 ⟶ 1,937:
main = do
print $ vanEck 10
print $ drop 990 (vanEck 1000)</langsyntaxhighlight>
{{Out}}
<pre>[0,0,1,0,2,0,2,2,1,6]
Line 1,460 ⟶ 1,944:
And if we wanted to look a little further than the 1000th term, we could accumulate a Map of most recently seen positions to improve performance:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}
 
import qualified Data.Map.Strict as M hiding (drop)
import Data.List (mapAccumL)
import qualified Data.Map.Strict as M hiding (drop)
import Data.Maybe (maybe)
 
--------------------- VAN ECK SEQUENCE -------------------
 
vanEck :: [Int]
Line 1,470 ⟶ 1,956:
where
go (x, dct) i =
((,) =<< (, M.insert x i dct)) (maybe 0 (i -) (M.lookup x dct))
(maybe 0 (i -) (M.lookup x dct))
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ print $
fmap
(drop . subtract 10 <*> flip take vanEck) <$>
((drop . subtract 10) <*> flip take vanEck)
[10, 1000, 10000, 100000, 1000000]</lang>
[10, 1000, 10000, 100000, 1000000]</syntaxhighlight>
{{Out}}
<pre>[0,0,1,0,2,0,2,2,1,6]
Line 1,486 ⟶ 1,975:
=={{header|J}}==
===The tacit verb (function)===
<langsyntaxhighlight lang="j">VanEck=. (, (<:@:# - }: i: {:))^:(]`0:)</langsyntaxhighlight>
 
===The output===
<langsyntaxhighlight lang="j"> VanEck 9
0 0 1 0 2 0 2 2 1 6
990 }. VanEck 999
4 7 30 25 67 225 488 0 10 136</langsyntaxhighlight>
===A structured derivation of the verb (function)===
<syntaxhighlight lang="j">
<lang j>
next =. <:@:# - }: i: {: NB. Next term of the sequence
VanEck=. (, next)^:(]`0:) f. NB. Appending terms and fixing the verb</langsyntaxhighlight>
 
=={{header|Java}}==
Use map to remember last seen index. Computes each value of the sequence in O(1) time.
<langsyntaxhighlight lang="java">
import java.util.HashMap;
import java.util.Map;
Line 1,533 ⟶ 2,022:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,565 ⟶ 2,054:
Either declaratively, without premature optimization:
{{Trans|Python}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,645 ⟶ 2,134:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>VanEck series:
Line 1,655 ⟶ 2,144:
or as a map-accumulation, building a look-up table:
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'"use strict'";
 
// vanEck :: Int -> [Int]
Line 1,662 ⟶ 2,151:
// First n terms of the vanEck series.
[0].concat(mapAccumL(
([x, seen],) => i) => {
const
prev = seen[x],
Line 1,668 ⟶ 2,157:
i - prev
) : 0;
 
return [
[v, (seen[x] = i, seen)], v
];
}, [0, {}],)(
[0, {}]
 
enumFromTo(1, n - 1)(
enumFromTo(1)(n - 1)
)[1]);
 
 
// ----------------------- TEST ------------------------
const main = () =>
fTable(
'"Terms of the VanEck series:\n',"
n => str(n - 10) + '-' + str(n),
xsn => JSON.stringify(xs.slice`${str(n - 10)}-${str(n),}`
vanEck,)(
[10,xs 1000,=> 10000]JSON.stringify(xs.slice(-10))
)(
vanEck
)([10, 1000, 10000]);
 
 
Line 1,690 ⟶ 2,184:
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
 
 
// fTable :: String -> (a -> String) -> (b -> String) ->
// fTable :: String -> (a -> bString) -> [a] -> String
const// fTable(b =-> (s,String) xShow,-> fxShow,(a f,-> xsb) =-> [a] -> {String
const fTable = s =>
// Heading -> x display function ->
// fx display function ->
// f -> values -> tabular string
constxShow => fxShow => f => xs => {
ys = xs.map(xShow),const
w = Math.max(... ys.map(x => xxs.length)map(xShow);,
w = Math.max(...ys.map(y => [...y].length)),
return s + '\n' + zipWith(
(a, b) => a.padStart(w, 'table ')= + ' -> ' + b,zipWith(
ys a => b => `${a.padStart(w, " ")} -> ${b}`
xs.map(x => fxShow )(f(x))ys)(
) xs.joinmap('\n'x => fxShow(f(x)));
).join("\n");
};
 
return `${s}\n${table}`;
// Map-accumulation is a combination of map and a catamorphism;
};
// it applies a function to each element of a list, passing an accumulating
// parameter from left to right, and returning a final value of this
// accumulator together with the new list.
 
// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
const mapAccumL = (f, acc, xs) =>
xs.reduce((a, x, i) => {
const pair = f(a[0], x, i);
return [pair[0], a[1].concat(pair[1])];
}, [acc, []]);
 
// replicatemapAccumL :: Int(acc -> ax -> [a](acc, y)) -> acc ->
const// replicate[x] =-> (nacc, x[y]) =>
const mapAccumL = f Array.from({=>
// A tuple of length:an naccumulation and a list
},// ()obtained =>by x);a combined map and fold,
// with accumulation from left to right.
acc => xs => [...xs].reduce(
([a, bs], x) => second(
v => bs.concat(v)
)(
f(a)(x)
),
[acc, []]
);
 
 
// second :: (a -> b) -> ((c, a) -> (c, b))
const second = f =>
// A function over a simple value lifted
// to a function over a tuple.
// f (a, b) -> (a, f(b))
([x, y]) => [x, f(y)];
 
 
// str :: a -> String
const str = x => x.toString();
 
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) => {
// A list constructed by zipping with a
const
// custom function, rather than with the
lng = Math.min(xs.length, ys.length),
// default tuple as = xsconstructor.slice(0, lng),
xs => bsys => ysxs.slicemap(0, lng);
return Array.from (x, i) => f(x)({ys[i])
length: lng).slice(
}, (_, i) => f(as[i]0, bs[i]Math.min(xs.length, i)ys.length);
} );
 
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Terms of the VanEck series:
Line 1,754 ⟶ 2,260:
 
=={{header|jq}}==
<syntaxhighlight lang="text"># Input: an array
# If the rightmost element, .[-1], does not occur elsewhere, return 0;
# otherwise return the "depth" of its rightmost occurrence in .[0:-2]
Line 1,773 ⟶ 2,279:
# The task:
[vanEck(10)], [vanEck(1000)][990:1001]
</syntaxhighlight>
</lang>
=== Output ===
<syntaxhighlight lang="text">[0,0,1,0,2,0,2,2,1,6]
[4,7,30,25,67,225,488,0,10,136]</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function vanecksequence(N, startval=0)
ret = zeros(Int, N)
ret[1] = startval
Line 1,793 ⟶ 2,299:
println(vanecksequence(10))
println(vanecksequence(1000)[991:1000])
</langsyntaxhighlight>{{out}}
<pre>
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6]
Line 1,799 ⟶ 2,305:
</pre>
Alternate version, with a Dict for memoization (output is the same):
<langsyntaxhighlight lang="julia">function vanecksequence(N, startval=0)
ret = zeros(Int, N)
ret[1] = startval
Line 1,811 ⟶ 2,317:
ret
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun main() {
println("First 10 terms of Van Eck's sequence:")
vanEck(1, 10)
Line 1,837 ⟶ 2,343:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 10 terms of Van Eck's sequence:
Line 1,864 ⟶ 2,370:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return a table of the first n values of the Van Eck sequence
function vanEck (n)
local seq, foundAt = {0}
Line 1,895 ⟶ 2,401:
local sequence = vanEck(1000)
showValues(sequence, 1, 10)
showValues(sequence, 991, 1000)</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE VANECK
.MCALL .TTYOUT,.EXIT
 
; CALCULATE VAN ECK SEQUENCE
VANECK::MOV #ECKBUF,R5
MOV R5,R0
CLR (R0)
1$: MOV R0,R1
BR 3$
2$: CMP (R0),(R1)
BEQ 4$
3$: SUB #2,R1
CMP R1,R5
BGE 2$
CLR R3
BR 5$
4$: MOV R0,R3
SUB R1,R3
ASR R3
5$: ADD #2,R0
MOV R3,(R0)
CMP R0,#BUFEND
BLE 1$
 
; PRINT VALUES
MOV #ECKBUF,R3
JSR PC,PR10
MOV #ECKBUF+<2*^D990>,R3
JSR PC,PR10
.EXIT
 
; PRINT 10 VALUES STARTING AT R3
PR10: MOV #^D10,R4
1$: MOV (R3)+,R0
JSR PC,PR0
SOB R4,1$
MOV #15,R0
.TTYOUT
MOV #12,R0
.TTYOUT
RTS PC
 
; PRINT NUMBER IN R0 AS DECIMAL
PR0: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .ASCIZ / /
.EVEN
 
ECKBUF: .BLKW ^D1000
BUFEND = .
.END VANECK</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION E(1000)
E(0)=0
Line 1,916 ⟶ 2,489:
S PRINT FORMAT FMT, I, E(I), I+990, E(I+990)
VECTOR VALUES FMT = $2HE(,I3,2H)=,I3,S5,2HE(,I3,2H)=,I3*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre>E( 0)= 0 E(990)= 4
Line 1,929 ⟶ 2,502:
E( 9)= 6 E(999)=136</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
TakeList[Nest[If[MemberQ[#//Most, #//Last], Join[#, Length[#] - Last@Position[#//Most, #//Last]], Append[#, 0]]&, {0}, 999], {10, -10}] // Column</syntaxhighlight>
{{out}}
<pre>{0,0,1,0,2,0,2,2,1,6}
{4,7,30,25,67,225,488,0,10,136}</pre>
 
=={{header|MathematicaMiranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
<lang Mathematica>
main = [ Stdout (show list ++ "\n")
TakeList[Nest[If[MemberQ[#//Most, #//Last], Join[#, Length[#] - Last@Position[#//Most, #//Last]], Append[#, 0]]&, {0}, 999], {10, -10}] // Column
| list <- [take 10 eck, take 10 (drop 990 eck)]
</lang>
]
 
eck :: [num]
eck = 0 : map item [1..]
where item n = find last (tl sofar)
where sofar = reverse (take n eck)
last = hd sofar
 
find :: *->[*]->num
find i = find' 1
where find' n [] = 0
find' n (a:as) = n, if a = i
= find' (n+1) as, otherwise</syntaxhighlight>
{{out}}
<pre>[0,0,1,0,2,0,2,2,1,6]
<pre>
{0[4,07,130,025,267,0225,2488,20,110,6}136]
{4,7,30,25,67,225,488,0,10,136}
</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE VanEck;
FROM InOut IMPORT WriteCard, WriteLn;
 
VAR i, j: CARDINAL;
eck: ARRAY [1..1000] OF CARDINAL;
 
BEGIN
FOR i := 1 TO 1000 DO
eck[i] := 0;
END;
FOR i := 1 TO 999 DO
j := i-1;
WHILE (j > 0) AND (eck[i] <> eck[j]) DO
DEC(j);
END;
IF j <> 0 THEN
eck[i+1] := i-j;
END;
END;
FOR i := 1 TO 10 DO
WriteCard(eck[i], 4);
END;
WriteLn();
FOR i := 991 TO 1000 DO
WriteCard(eck[i], 4);
END;
WriteLn();
END VanEck.</syntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">const max = 1000
var a: array[max, int]
for n in countup(0, max - 2):
Line 1,953 ⟶ 2,578:
echo a[..9]
echo "\nTerms 991 to 1000 of the sequence are:"
echo a[990..^1]</langsyntaxhighlight>
 
{{out}}
Line 1,968 ⟶ 2,593:
Running once through the list of last positions maybe faster
[https://tio.run/##jVTbThsxEH3frxipDyR0k2xAFWjToAIFNVKBqoEKKULIOLPEZfFubW9CqPrrTT129sKlVf2wlzlzOz5jZzffkZtOzjRnaSfJ@WqVq@xWsXv4xuQRvxsEP4NN2I8BzmcIiVDagEF1D0LDI6qsG4BdXzFHZnCaLoHlebqMnZXWKAFjA1PWiNuUuNgEkzlE448CJbcfGSRMkU3GwUEM1aLCEh9eq0vrzEaohdAYB4f/ipplC1fghvE7W8Va6qYyzguFU8gVzkVWaEyX3V@BfWtbRi91YUSqBwHPpDbWcrJ/eXpxAkPY3tre7e/svBv0ev0oijarx8B7jY@OTkcfL61nqw96lsJOu9MfBHOmLP4l0weYZAohBqYUW0KWwMVImu0tCv9suxsjSijhSdTt1jmvaudeD7hQvEiJXZEkSNkpdDR9CD@xObo0MVwInzuwEnOcWsZwVrgarUNp4lNmxBxtynbZ4SgcX9scNrTCBsEB3gpJ4DHYKNhr8iTxnDCExMMGRIzsLKxDqqZeRJQI@a@LD0suT2wt99U5dO26bjz0HqI6qZDc@4V1J879OFMgKE2f5pBqTzNrLrkBLJQw2NqAjbDUYeISXbnwdR9VG2/7bdg//fiML8opvVyq1DJy/43NP0dtWifsgd4kgNen2n0RMm5kcR/aQRFeiVpDgNzPT5h7dfMSKTkcizTlM6ZaJYFwLB7xLKn@2@GbyLGpR2QI0Xoz67b2ynGvdvXocnRObr2eRpOivDUzYHIKPEXXeGVtVSMe@kqvIXUl61LzomY@VF6T6Mpx9kPjsEoXD1WD4Rh4benL72EFKHdTORr22GjLHOYsLdBZ8obOFOFj/19wyokP7F5IunsWPrM91cY2nRWGzjdnfOaLkapUZM134otd@TzPjOQnPGK1cZHD5pw/oeksmGp8AYkOhQ6qoyGcKIW0t5vdsb2G6M/HQnTcZq4PojuT1@5E@pkuZ85NdD9qAwyqq4WuQ6/@GqW/BlpDfs4a4NbuX6eGwmzt7mr1mycpu9Wrztn2Hw Try it online!] takes only 1.4 s for 32,381,775
<langsyntaxhighlight lang="pascal">program VanEck;
{
* A: The first term is zero.
Line 2,066 ⟶ 2,691:
Test(MAXNUM); OutSeen(28);
setlength(PosBefore,0);
end.</langsyntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
Line 2,075 ⟶ 2,700:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,110 ⟶ 2,735:
Terms 991 through 1000: @{[@seq[990..999]]}
END
}</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">Van Eck sequence OEIS:A181391; with the first term: 0
Line 2,152 ⟶ 2,777:
and likewise this can easily create a 32-million-long table in under 2s.<br>
While dictionaries are pretty fast, there is a huge overhead adding/updating millions of entries compared to a flat list of int.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">van_eck</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">),</span>
Line 2,166 ⟶ 2,791:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first ten terms of the Van Eck sequence are:%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">van_eck</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">10</span><span style="color: #0000FF;">]})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Terms 991 to 1000 of the sequence are:%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">van_eck</span><span style="color: #0000FF;">[</span><span style="color: #000000;">991</span><span style="color: #0000FF;">..</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">]})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,172 ⟶ 2,797:
Terms 991 to 1000 of the sequence are:{4,7,30,25,67,225,488,0,10,136}
</pre>
 
=={{header|Picat}}==
{{trans|FreeBasic}}
Note: Picat is 1-based.
<syntaxhighlight lang="picat">main =>
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.</syntaxhighlight>
 
{{out}}
<pre>{0,0,1,0,2,0,2,2,1,6}
{4,7,30,25,67,225,488,0,10,136}</pre>
 
===Recursive===
Same idea but recursive.
<syntaxhighlight lang="picat">main =>
L = a(1000),
println(L[1..10]),
println(L[991..1000]),
nl.
 
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).</syntaxhighlight>
 
{{out}}
<pre>{0,0,1,0,2,0,2,2,1,6,0}
{4,7,30,25,67,225,488,0,10,136}
</pre>
 
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,219 ⟶ 2,885:
CALL PRINT$SLICE(.ECK(990), 10);
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 2,226 ⟶ 2,892:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">van_eck_init(v(0, 0, _assoc)):-
empty_assoc(_assoc).
 
Line 2,273 ⟶ 2,939:
write_list(1, 10, Seq),
writeln('Terms 991 to 1000 of the Van Eck sequence:'),
write_list(991, 1000, Seq).</langsyntaxhighlight>
 
{{out}}
Line 2,285 ⟶ 2,951:
=={{header|Python}}==
===Python: Using a dict===
<langsyntaxhighlight lang="python">def van_eck():
n, seen, val = 0, {}, 0
while True:
Line 2,296 ⟶ 2,962:
if __name__ == '__main__':
print("Van Eck: first 10 terms: ", list(islice(van_eck(), 10)))
print("Van Eck: terms 991 - 1000:", list(islice(van_eck(), 1000))[-10:])</langsyntaxhighlight>
 
{{out}}
Line 2,305 ⟶ 2,971:
===Python: List based===
The following alternative stores the sequence so far in a list <code>seen</code> rather than the first example that just stores ''last occurrences'' in a dict.
<langsyntaxhighlight lang="python">def van_eck():
n = 0
seen = [0]
Line 2,316 ⟶ 2,982:
val = 0
seen.insert(0, val)
n += 1</langsyntaxhighlight>
 
{{out}}
Line 2,326 ⟶ 2,992:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Van Eck sequence'''
 
from functools import reduce
Line 2,424 ⟶ 3,090:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Terms of the Van Eck sequence:
Line 2,434 ⟶ 3,100:
Or if we lose sight, for a moment, of the good advice of Donald Knuth, and fall into optimising more than is needed for the first 1000 terms, then we can define the vanEck series as a map accumulation over a range, with an array of positions as the accumulator.
 
<langsyntaxhighlight lang="python">'''Van Eck series by map-accumulation'''
 
from functools import reduce
Line 2,512 ⟶ 3,178:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>The last 10 of the first N vanEck terms:
Line 2,519 ⟶ 3,185:
N=1000 -> [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
N=10000 -> [7, 43, 190, 396, 2576, 3142, 0, 7, 7, 1]</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ ' [ 0 ]
swap 1 - times
[ dup behead swap find
1+ 2dup swap found *
swap join ]
reverse ] is van-eck ( n --> [ )
 
10 van-eck echo cr
1000 van-eck -10 split echo drop</syntaxhighlight>
 
{{out}}
 
<pre>[ 0 0 1 0 2 0 2 2 1 6 ]
[ 4 7 30 25 67 225 488 0 10 136 ]</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/stream)
 
Line 2,536 ⟶ 3,219:
 
"First 10 terms:" (get 0 10 (van-eck))
"Terms 991 to 1000 terms:" (get 990 1000 (van-eck)) ; counting from 0</langsyntaxhighlight>
 
{{out}}
Line 2,564 ⟶ 3,247:
Implemented as lazy, extendable lists.
 
<syntaxhighlight lang="raku" perl6line>sub n-van-ecks ($init) {
$init, -> $i, {
state %v;
Line 2,596 ⟶ 3,279:
Terms 991 through 1000: {@seq[990..999]}
END
}</langsyntaxhighlight>
 
{{out}}
Line 2,642 ⟶ 3,325:
First 10 terms: 8 0 0 1 0 2 0 2 2 1
Terms 991 through 1000: 16 183 0 6 21 10 249 0 5 48</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Eck 1000>: e.Eck
, <First 10 e.Eck>: (e.First10) e.Rest1
, <Last 10 e.Eck>: (e.Rest2) e.Last10
= <Prout e.First10>
<Prout e.Last10>;
};
 
Eck {
s.N = <Reverse <Repeat s.N EckStep>>;
};
 
Reverse {
= ;
e.X s.I = s.I <Reverse e.X>;
};
 
Repeat {
0 s.F e.X = e.X;
s.N s.F e.X = <Repeat <- s.N 1> s.F <Mu s.F e.X>>;
};
 
EckStep {
= 0;
s.N e.X, e.X: e.F s.N e.R,
<Lenw e.F>: s.M e.F = <+ s.M 1> s.N e.X;
s.N e.X = 0 s.N e.X;
};</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|REXX}}==
Line 2,647 ⟶ 3,363:
This REXX version allows the specification of the &nbsp; ''start'' &nbsp; and &nbsp; ''end'' &nbsp; of the &nbsp; ''Van Eck'' &nbsp; sequence &nbsp; (to be displayed) &nbsp; as
<br>well as the initial starting element &nbsp; (the default is zero).
<langsyntaxhighlight lang="rexx">/*REXX pgm generates/displays the 'start ──► end' elements of the Van Eck sequence.*/
parse arg LO HI $ . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 2,656 ⟶ 3,372:
end /*HI-1*/ /*REVERSE allows backwards search in $.*/
/*stick a fork in it, we're all done. */
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' subword($,LO,HI-LO+1)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,673 ⟶ 3,389:
This REXX version &nbsp; (which uses a dictionary) &nbsp; is about &nbsp; '''20,000''' &nbsp; times faster &nbsp; (when using larger numbers) &nbsp; than
<br>using a list &nbsp; (in finding the previous location of an "old" number (term).
<langsyntaxhighlight lang="rexx">/*REXX pgm generates/displays the 'start ──► end' elements of the Van Eck sequence.*/
parse arg LO HI sta . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 2,686 ⟶ 3,402:
do j=LO to HI-1; out= out $.j /*build a list for the output display. */
end /*j*/ /*stick a fork in it, we're all done. */
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' out</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP DUP SIZE GET → list item
≪ list SIZE 1 - DUP 1 CF
'''WHILE''' DUP 1 FC? AND '''REPEAT '''
'''IF''' list OVER GET item == '''THEN''' 1 SF '''END'''
1 - '''END '''
- 1 FS? *
≫ ≫ ''''LastOcc'''' STO
≪ { 0 } 2 ROT '''START''' DUP '''LastOcc''' + '''NEXT'''
≫ ''''VANECK'''' STO
OVER SIZE { } ROT ROT '''FOR''' j
OVER j GET + '''NEXT'''
SWAP DROP
≫ ''''LASTL'''' STO
|
''( { sequence } -- pos_from_end )''
Store sequence and last item
Initialize loop
Scan sequence backwards
if item found then break
decrement
Return pos or 0 if not found
''( n -- { 0..VanEck(n) } )''
''( { n items } m -- { m..n } )''
Extract items from mth position
|}
{{in}}
<pre>
10 VANECK
1000 VANECK 991 LASTL
</pre>
{{out}}
<pre>
2: { 0 0 1 0 2 0 2 2 1 6 }
1: { 4 7 30 25 67 225 488 0 10 136 }
</pre>
 
=={{header|Ruby}}==
===Ruby: Using an Array===
<langsyntaxhighlight lang="ruby">van_eck = Enumerator.new do |y|
ar = [0]
loop do
Line 2,701 ⟶ 3,471:
ve = van_eck.take(1000)
p ve.first(10), ve.last(10)
</syntaxhighlight>
</lang>
{{out}}
<pre>[0, 0, 1, 0, 2, 0, 2, 2, 1, 6]
Line 2,708 ⟶ 3,478:
 
===Ruby: Using a Hash===
<langsyntaxhighlight lang="ruby">class VenEch
include Enumerable
Line 2,730 ⟶ 3,500:
ve = VenEch.new.take(1000)
p ve.first(10), ve.last(10)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,738 ⟶ 3,508:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn van_eck_sequence() -> impl std::iter::Iterator<Item = i32> {
let mut index = 0;
let mut last_term = 0;
Line 2,768 ⟶ 3,538:
}
println!();
}</langsyntaxhighlight>
 
{{out}}
Line 2,779 ⟶ 3,549:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
object VanEck extends App {
 
Line 2,797 ⟶ 3,567:
println(s"Terms 991 to 1000 are ${vanEck1000.drop(990)}.")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,803 ⟶ 3,573:
Terms 991 to 1000 are List(4, 7, 30, 25, 67, 225, 488, 0, 10, 136).
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program vaneck;
eck := [0];
 
loop for i in [1..999] do
prev := eck(..#eck-1);
eck(i+1) := i - (max/[j : e=prev(j) | e=eck(i)] ? i);
end loop;
 
print(eck(1..10));
print(eck(991..1000));
end program;</syntaxhighlight>
{{out}}
<pre>[0 0 1 0 2 0 2 2 1 6]
[4 7 30 25 67 225 488 0 10 136]</pre>
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight lang="snobol4"> define('eck(n)i,j') :(eck_end)
eck eck = array(n,0)
i = 0
Line 2,822 ⟶ 3,608:
output = list(ecks, 1, 10)
output = list(ecks, 991, 1000)
end</langsyntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
Line 2,828 ⟶ 3,614:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func van_eck(n) {
 
var seen = Hash()
Line 2,844 ⟶ 3,630:
 
say van_eck(10)
say van_eck(1000).slicelast(991-1, 1000-110)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,853 ⟶ 3,639:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">struct VanEckSequence: Sequence, IteratorProtocol {
private var index = 0
private var lastTerm = 0
Line 2,881 ⟶ 3,667:
print(n, terminator: " ")
}
print()</langsyntaxhighlight>
 
{{out}}
Line 2,892 ⟶ 3,678:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">## Mathematically, the first term has index "0", not "1". We do that, also.
 
set ::vE 0
Line 2,915 ⟶ 3,701:
 
show vanEck 0 9
show vanEck 990 999</langsyntaxhighlight>
{{out}}
vanEck(0..9) = 0 0 1 0 2 0 2 2 1 6
vanEck(990..999) = 4 7 30 25 67 225 488 0 10 136
 
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
F ← ⊂⟨0◌|+1⟩>⊙(-1⧻),,⊗⊃⊢(↘1). # Generator function (prepends).
⇌⍥F999 [0] # Generate 1000 terms and flip.
⊃(&p↙¯)(&p↙∘)10 # Print first and last 10 terms.
</syntaxhighlight>
{{out}}
<pre>
[0 0 1 0 2 0 2 2 1 6]
[4 7 30 25 67 225 488 0 10 136]
</pre>
 
=={{header|Visual Basic .NET}}==
{{Trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Linq
Module Module1
Dim h() As Integer
Line 2,935 ⟶ 3,733:
Next : a = b : b = c : Next : sho(0) : sho(990)
End Sub
End Module </langsyntaxhighlight>
{{out}}
Same as C#.
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
max := 1000
mut a := []int{len: max} // all zero by default
for n in 0..max-1 {
for m := n - 1; m >= 0; m-- {
if a[m] == a[n] {
a[n+1] = n - m
break
}
}
}
println("The first ten terms of the Van Eck sequence are:")
println(a[..10])
println("\nTerms 991 to 1000 of the sequence are:")
println(a[990..])
}</syntaxhighlight>
 
{{out}}
<pre>
The first ten terms of the Van Eck sequence are:
[0 0 1 0 2 0 2 2 1 6]
 
Terms 991 to 1000 of the sequence are:
[4 7 30 25 67 225 488 0 10 136]
</pre>
 
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">1000 :1)=0
1010 I=1
1020 J=I-1
1030 #=J=0*1090
1040 #=:J)=:I)*1070
1050 J=J-1
1060 #=1030
1070 :I+1)=I-J
1080 #=1100
1090 :I+1)=0
1100 I=I+1
1110 #=I<1000*1020
1120 I=1
1130 #=1170
1140 I=991
1150 #=1170
1160 #=9999
1170 R=!
1180 N=10
1190 ?=:I)
1200 $=32
1210 I=I+1
1220 N=N-1
1230 #=N=0=0*1190
1240 ?=""
1250 #=R</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight javascriptlang="wren">var max = 1000
var a = List.filled(max, 0)
var seen = {}
Line 2,952 ⟶ 3,809:
System.print(a[0...10])
System.print("\nTerms 991 to 1000 of the sequence are:")
System.print(a[990..-1])</langsyntaxhighlight>
 
{{out}}
Line 2,961 ⟶ 3,818:
Terms 991 to 1000 of the sequence are:
[4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int Seq(1001), Back, N, M, Term;
 
func New; \Return 'true' if last term is new to sequence
[for Back:= N-2 downto 1 do
if Seq(Back) = Seq(N-1) then return false;
return true;
];
 
func VanEck; \Return term of Van Eck sequence
[Seq(N):= if New then 0 else N-Back-1;
N:= N+1;
return Seq(N-1);
];
 
[N:= 1;
for M:= 1 to 1000 do
[Term:= VanEck;
if M<=10 or M>=991 then
[IntOut(0, Term);
if M=10 or M=1000 then Crlf(0) else Text(0, ", ");
];
];
]</syntaxhighlight>
 
{{out}}
<pre>
0, 0, 1, 0, 2, 0, 2, 2, 1, 6
4, 7, 30, 25, 67, 225, 488, 0, 10, 136
</pre>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">fcn vanEck(startAt=0){ // --> iterator
(startAt).walker(*).tweak(fcn(n,seen,rprev){
prev,t := rprev.value, n - seen.find(prev,n);
Line 2,972 ⟶ 3,860:
t
}.fp1(Dictionary(),Ref(startAt))).push(startAt)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in (9){
ve:=vanEck(n);
println("The first ten terms of the Van Eck (%d) sequence are:".fmt(n));
Line 2,979 ⟶ 3,867:
println(" Terms 991 to 1000 of the sequence are:");
println("\t",ve.drop(990-10).walk(10).concat(","));
}</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
62

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.