Van Eck sequence: Difference between revisions
Added Algol 68 |
Walterpachl (talk | contribs) →{{header|REXX}}: reformat and make it ooRexx compatible |
||
(49 intermediate revisions by 24 users not shown) | |||
Line 7: | Line 7: | ||
B: The next term is zero. |
B: The next term is zero. |
||
Otherwise: |
Otherwise: |
||
C: The next term is how far back this last term occured |
C: The next term is how far back this last term occured previously. |
||
</pre> |
</pre> |
||
Line 20: | Line 20: | ||
Using B: |
Using B: |
||
:<code>0 0 1 0</code> |
:<code>0 0 1 0</code> |
||
Using C: (zero last |
Using C: (zero last occurred two steps back - before the one) |
||
:<code>0 0 1 0 2</code> |
:<code>0 0 1 0 2</code> |
||
Using B: |
Using B: |
||
:<code>0 0 1 0 2 0</code> |
:<code>0 0 1 0 2 0</code> |
||
Using C: (two last |
Using C: (two last occurred two steps back - before the zero) |
||
:<code>0 0 1 0 2 0 2 2</code> |
:<code>0 0 1 0 2 0 2 2</code> |
||
Using C: (two last |
Using C: (two last occurred one step back) |
||
:<code>0 0 1 0 2 0 2 2 1</code> |
:<code>0 0 1 0 2 0 2 2 1</code> |
||
Using C: (one last appeared six steps back) |
Using C: (one last appeared six steps back) |
||
:<code>0 0 1 0 2 0 2 2 1 6</code> |
:<code>0 0 1 0 2 0 2 2 1 6</code> |
||
... |
... |
||
;Task: |
;Task: |
||
# Create a function/ |
# Create a function/procedure/method/subroutine/... to generate the Van Eck sequence of numbers. |
||
# Use it to display here, on this page: |
# Use it to display here, on this page: |
||
:# The first ten terms of the sequence. |
:# The first ten terms of the sequence. |
||
:# Terms 991 - to - 1000 of the sequence. |
:# Terms 991 - to - 1000 of the sequence. |
||
;References: |
;References: |
||
* [https://www.youtube.com/watch?v=etMJxB-igrc Don't Know (the Van Eck Sequence) - Numberphile video]. |
* [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]]. |
* [[wp:Van_Eck%27s_sequence|Wikipedia Article: Van Eck's Sequence]]. |
||
* [[ |
* [[OEIS:A181391| OEIS sequence: A181391]]. |
||
<br> |
<br><br> |
||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python: List based}} |
{{trans|Python: List based}} |
||
< |
<syntaxhighlight lang="11l">F van_eck(c) |
||
[Int] r |
[Int] r |
||
V n = 0 |
V n = 0 |
||
Line 63: | Line 66: | ||
print(‘Van Eck: first 10 terms: ’van_eck(10)) |
print(‘Van Eck: first 10 terms: ’van_eck(10)) |
||
print(‘Van Eck: terms 991 - 1000: ’van_eck(1000)[(len)-10..])</ |
print(‘Van Eck: terms 991 - 1000: ’van_eck(1000)[(len)-10..])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 72: | Line 75: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="8080asm"> org 100h |
||
lxi h,ecks ; Zero out 2000 bytes |
lxi h,ecks ; Zero out 2000 bytes |
||
lxi b,0 |
lxi b,0 |
||
Line 181: | Line 184: | ||
db '.....' |
db '.....' |
||
buf: db ' $' |
buf: db ' $' |
||
ecks: equ $</ |
ecks: equ $</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 187: | Line 190: | ||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<syntaxhighlight lang="asm">LIMIT: equ 1000 |
||
cpu 8086 |
cpu 8086 |
||
org 100h |
org 100h |
||
Line 248: | Line 251: | ||
buf: db ' $' |
buf: db ' $' |
||
section .bss |
section .bss |
||
eck: resw LIMIT</ |
eck: resw LIMIT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 255: | Line 258: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program vanEckSerie64.s */ |
/* program vanEckSerie64.s */ |
||
Line 356: | Line 359: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
0 0 1 0 2 0 2 2 1 6 |
0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136 |
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> |
</pre> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Van_Eck_Sequence is |
procedure Van_Eck_Sequence is |
||
Line 397: | Line 469: | ||
Show (First => 1, Last => 10); |
Show (First => 1, Last => 10); |
||
Show (First => 991, Last => 1_000); |
Show (First => 991, Last => 1_000); |
||
end Van_Eck_Sequence;</ |
end Van_Eck_Sequence;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 404: | Line 476: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight 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 # |
# terms are 0 if the previous was the first appearance of the element # |
||
# or how far back in the sequence the last element appeared # |
# or how far back in the sequence the last element appeared # |
||
Line 430: | Line 502: | ||
FOR i FROM UPB seq - 9 TO UPB seq DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD; |
FOR i FROM UPB seq - 9 TO UPB seq DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD; |
||
print( ( newline ) ) |
print( ( newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 438: | Line 510: | ||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
< |
<syntaxhighlight lang="algol">begin |
||
integer array eck[1:1000]; |
integer array eck[1:1000]; |
||
integer i, j; |
integer i, j; |
||
Line 460: | Line 532: | ||
writeon(eck[i]); |
writeon(eck[i]); |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 0 1 0 2 0 2 2 1 6 |
<pre> 0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136</pre> |
4 7 30 25 67 225 488 0 10 136</pre> |
||
=={{header|ALGOL W}}== |
|||
{{Trans|ALGOL 68}} |
|||
<syntaxhighlight 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 % |
|||
% sets s to the first n elements of the Van Eck sequence % |
|||
procedure VanEck ( integer array s ( * ) ; integer value n ) ; |
|||
begin |
|||
integer array pos ( 0 :: n ); |
|||
for i := 1 until n do s( i ) := 0; |
|||
for i := 0 until n do pos( i ) := 0; |
|||
for i := 2 until n do begin |
|||
integer j, prev; |
|||
j := i - 1; |
|||
prev := s( j ); |
|||
if pos( prev ) not = 0 then begin |
|||
% not a new element % |
|||
s( i ) := j - pos( prev ) |
|||
end if_pos_prev_ne_0 ; |
|||
pos( prev ) := j |
|||
end for_j; |
|||
end VanEck ; |
|||
% construct the first 1000 terms of the sequence % |
|||
integer MAX_VAN_ECK; |
|||
MAX_VAN_ECK := 1000; |
|||
begin |
|||
integer array seq ( 1 :: MAX_VAN_ECK ); |
|||
VanEck( seq, MAX_VAN_ECK ); |
|||
% show the first and last 10 elements % |
|||
for i := 1 until 10 do writeon( i_w := 1, s_w := 0, " ", seq( i ) ); |
|||
write(); |
|||
for i := MAX_VAN_ECK - 9 until MAX_VAN_ECK do writeon( i_w := 1, s_w := 0, " ", seq( i ) ); |
|||
write() |
|||
end |
|||
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|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}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">(10∘↑,[.5]¯10∘↑)(⊢,(⊃∘⌽∊¯1∘↓)∧(1↓⌽)⍳⊃∘⌽)⍣999⊢,0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 475: | Line 614: | ||
===Functional=== |
===Functional=== |
||
AppleScript is not the tool for the job, but here is a quick assembly from ready-made parts: |
AppleScript is not the tool for the job, but here is a quick assembly from ready-made parts: |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |
||
use scripting additions |
use scripting additions |
||
Line 647: | Line 786: | ||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
s |
s |
||
end unlines</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>First 10 terms: |
<pre>First 10 terms: |
||
Line 658: | Line 797: | ||
On the contrary, it's right up AppleScript's street. |
On the contrary, it's right up AppleScript's street. |
||
< |
<syntaxhighlight lang="applescript">on vanEckSequence(limit) |
||
script o |
script o |
||
property sequence : {} |
property sequence : {} |
||
Line 685: | Line 824: | ||
-- Task code: |
-- Task code: |
||
tell vanEckSequence(1000) to return {items 1 thru 10, items 991 thru 1000}</ |
tell vanEckSequence(1000) to return {items 1 thru 10, items 991 thru 1000}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">{{0, 0, 1, 0, 2, 0, 2, 2, 1, 6}, {4, 7, 30, 25, 67, 225, 488, 0, 10, 136}}</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program vanEckSerie.s */ |
/* program vanEckSerie.s */ |
||
Line 795: | Line 934: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
0 0 1 0 2 0 2 2 1 6 |
0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136 |
4 7 30 25 67 225 488 0 10 136 |
||
</pre> |
</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}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f VAN_ECK_SEQUENCE.AWK |
# syntax: GAWK -f VAN_ECK_SEQUENCE.AWK |
||
# converted from Go |
# converted from Go |
||
Line 825: | Line 995: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 833: | Line 1,003: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QBasic}} |
|||
<lang basic>10 DEFINT A-Z |
|||
<syntaxhighlight lang="basic">10 DEFINT A-Z |
|||
20 DIM E(1000) |
20 DIM E(1000) |
||
30 FOR I=0 TO 999 |
30 FOR I=0 TO 999 |
||
Line 843: | Line 1,014: | ||
90 FOR I=0 TO 9: PRINT E(I);: NEXT |
90 FOR I=0 TO 9: PRINT E(I);: NEXT |
||
95 PRINT |
95 PRINT |
||
100 FOR I=990 TO 999: PRINT E(I);: NEXT</ |
100 FOR I=990 TO 999: PRINT E(I);: NEXT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 0 1 0 2 0 2 2 1 6 |
<pre> 0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136</pre> |
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}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let start() be |
let start() be |
||
Line 866: | Line 1,302: | ||
for i = 990 to 999 do writed(eck!i, 4) |
for i = 990 to 999 do writed(eck!i, 4) |
||
wrch('*N') |
wrch('*N') |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 0 1 0 2 0 2 2 1 6 |
<pre> 0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136</pre> |
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}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 893: | Line 1,338: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 905: | Line 1,350: | ||
=={{header|C#|CSharp}}== |
=={{header|C#|CSharp}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="csharp">using System.Linq; class Program { static void Main() { |
||
int a, b, c, d, e, f, g; int[] h = new int[g = 1000]; |
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++) |
for (a = 0, b = 1, c = 2; c < g; a = b, b = c++) |
||
Line 911: | Line 1,356: | ||
if (f == h[d--]) { h[c] = e; break; } |
if (f == h[d--]) { h[c] = e; break; } |
||
void sho(int i) { System.Console.WriteLine(string.Join(" ", |
void sho(int i) { System.Console.WriteLine(string.Join(" ", |
||
h.Skip(i).Take(10))); } sho(0); sho(990); } }</ |
h.Skip(i).Take(10))); } sho(0); sho(990); } }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 918: | Line 1,363: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <map> |
#include <map> |
||
Line 951: | Line 1,396: | ||
std::cout << '\n'; |
std::cout << '\n'; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 962: | Line 1,407: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn van-eck |
||
([] (van-eck 0 0 {})) |
([] (van-eck 0 0 {})) |
||
([val n seen] |
([val n seen] |
||
Line 973: | Line 1,418: | ||
(println "First 10 terms:" (take 10 (van-eck))) |
(println "First 10 terms:" (take 10 (van-eck))) |
||
(println "Terms 991 to 1000 terms:" (take 10 (drop 990 (van-eck))))</ |
(println "Terms 991 to 1000 terms:" (take 10 (drop 990 (van-eck))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 10 terms: (0 0 1 0 2 0 2 2 1 6) |
<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> |
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}}== |
|||
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
|||
PROGRAM-ID. VAN-ECK. |
|||
DATA DIVISION. |
|||
WORKING-STORAGE SECTION. |
|||
01 CALCULATION. |
|||
02 ECK PIC 999 OCCURS 1000 TIMES. |
|||
02 I PIC 9999. |
|||
02 J PIC 9999. |
|||
01 OUTPUT-FORMAT. |
|||
02 ITEM PIC ZZ9. |
|||
02 IDX PIC ZZZ9. |
|||
PROCEDURE DIVISION. |
|||
B. PERFORM GENERATE-ECK. |
|||
PERFORM SHOW VARYING I FROM 1 BY 1 UNTIL I = 11. |
|||
PERFORM SHOW VARYING I FROM 991 BY 1 UNTIL I = 1001. |
|||
STOP RUN. |
|||
SHOW. |
|||
MOVE I TO IDX. |
|||
MOVE ECK(I) TO ITEM. |
|||
DISPLAY 'ECK(' IDX ') = ' ITEM. |
|||
GENERATE-ECK SECTION. |
|||
B. SET ECK(1) TO 0. |
|||
SET I TO 1. |
|||
PERFORM GENERATE-TERM |
|||
VARYING I FROM 2 BY 1 UNTIL I = 1001. |
|||
GENERATE-TERM SECTION. |
|||
B. SUBTRACT 2 FROM I GIVING J. |
|||
LOOP. |
|||
IF J IS LESS THAN 1 GO TO TERM-IS-NEW. |
|||
IF ECK(J) = ECK(I - 1) GO TO TERM-IS-OLD. |
|||
SUBTRACT 1 FROM J. |
|||
GO TO LOOP. |
|||
TERM-IS-NEW. |
|||
SET ECK(I) TO 0. |
|||
GO TO DONE. |
|||
TERM-IS-OLD. |
|||
COMPUTE ECK(I) = (I - J) - 1. |
|||
DONE. EXIT.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>ECK( 1) = 0 |
|||
ECK( 2) = 0 |
|||
ECK( 3) = 1 |
|||
ECK( 4) = 0 |
|||
ECK( 5) = 2 |
|||
ECK( 6) = 0 |
|||
ECK( 7) = 2 |
|||
ECK( 8) = 2 |
|||
ECK( 9) = 1 |
|||
ECK( 10) = 6 |
|||
ECK( 991) = 4 |
|||
ECK( 992) = 7 |
|||
ECK( 993) = 30 |
|||
ECK( 994) = 25 |
|||
ECK( 995) = 67 |
|||
ECK( 996) = 225 |
|||
ECK( 997) = 488 |
|||
ECK( 998) = 0 |
|||
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}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang="lisp"> |
|||
<lang Lisp> |
|||
;;Tested using CLISP |
;;Tested using CLISP |
||
Line 1,000: | Line 1,567: | ||
(format t "The first 10 elements are ~a~%" (VanEck 9)) |
(format t "The first 10 elements are ~a~%" (VanEck 9)) |
||
(format t "The 990-1000th elements are ~a~%" (nthcdr 990 (VanEck 999))) |
(format t "The 990-1000th elements are ~a~%" (nthcdr 990 (VanEck 999))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>The first 10 elements are (0 0 1 0 2 0 2 2 1 6) |
<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) |
The 990-1000th elements are (4 7 30 25 67 225 488 0 10 136) |
||
</pre> |
</pre> |
||
<syntaxhighlight lang="lisp"> |
|||
<lang Lisp> |
|||
(defun van-eck-nm-sequence (n m) |
(defun van-eck-nm-sequence (n m) |
||
(loop with ac repeat m |
(loop with ac repeat m |
||
Line 1,014: | Line 1,581: | ||
(format t "The first 10 elements are: ~{~a ~}~%" (van-eck-nm-sequence 1 10)) |
(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)) |
(format t "The 991-1000th elements are: ~{~a ~}" (van-eck-nm-sequence 991 1000)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>The first 10 elements are: 0 0 1 0 2 0 2 2 1 6 |
<pre>The first 10 elements are: 0 0 1 0 2 0 2 2 1 6 |
||
Line 1,020: | Line 1,587: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub print_list(ptr: [uint16], n: uint8) is |
sub print_list(ptr: [uint16], n: uint8) is |
||
Line 1,052: | Line 1,619: | ||
print_list(&eck[0], 10); |
print_list(&eck[0], 10); |
||
print_list(&eck[LIMIT-10], 10);</ |
print_list(&eck[LIMIT-10], 10);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 1,059: | Line 1,626: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
void vanEck(int firstIndex, int lastIndex) { |
void vanEck(int firstIndex, int lastIndex) { |
||
Line 1,083: | Line 1,650: | ||
writeln("Terms 991 to 1000 of Van Eck's sequence:"); |
writeln("Terms 991 to 1000 of Van Eck's sequence:"); |
||
vanEck(991, 1000); |
vanEck(991, 1000); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 10 terms of Van Eck's sequence: |
<pre>First 10 terms of Van Eck's sequence: |
||
Line 1,111: | Line 1,678: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
See [https://rosettacode.org/wiki/Van_Eck_sequence#Pascal Pascal]. |
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}}== |
=={{header|Dyalect}}== |
||
Line 1,116: | Line 1,716: | ||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="dyalect">let max = 1000 |
||
var a = Array. |
var a = Array.Empty(max, 0) |
||
for n in 0..(max-2) { |
for n in 0..(max-2) { |
||
var m = n - 1 |
var m = n - 1 |
||
Line 1,128: | Line 1,728: | ||
} |
} |
||
} |
} |
||
print("The first ten terms of the Van Eck sequence are: \(a[0..10])") |
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])")</ |
print("Terms 991 to 1000 of the sequence are: \(a[991..999].ToArray())")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first ten terms of the Van Eck sequence are: |
<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: |
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#}}== |
=={{header|F_Sharp|F#}}== |
||
===The function=== |
===The function=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Generate Van Eck's Sequence. Nigel Galloway: June 19th., 2019 |
// Generate Van Eck's Sequence. Nigel Galloway: June 19th., 2019 |
||
let ecK()=let n=System.Collections.Generic.Dictionary<int,int>() |
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) |
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=== |
===The Task=== |
||
;First 50 |
;First 50 |
||
< |
<syntaxhighlight lang="fsharp"> |
||
ecK() |> Seq.take 50 |> Seq.iter(printf "%d "); printfn "";; |
ecK() |> Seq.take 50 |> Seq.iter(printf "%d "); printfn "";; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,154: | Line 1,774: | ||
</pre> |
</pre> |
||
;50 from 991 |
;50 from 991 |
||
< |
<syntaxhighlight lang="fsharp"> |
||
ecK() |> Seq.skip 990 |> Seq.take 50|> Seq.iter(printf "%d "); printfn "";; |
ecK() |> Seq.skip 990 |> Seq.take 50|> Seq.iter(printf "%d "); printfn "";; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,167: | Line 1,787: | ||
</pre> |
</pre> |
||
===Alternative recursive procedure=== |
===Alternative recursive procedure=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
open System.Collections.Generic |
open System.Collections.Generic |
||
let VanEck() = |
let VanEck() = |
||
Line 1,189: | Line 1,809: | ||
VanEck() |> Seq.take 10 |> Seq.map (sprintf "%i") |> String.concat " " |> printfn "The first ten terms of the sequence : %s" |
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" |
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}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: assocs fry kernel make math namespaces prettyprint |
||
sequences ; |
sequences ; |
||
Line 1,203: | Line 1,823: | ||
] { } make ; |
] { } make ; |
||
1000 van-eck 10 [ head ] [ tail* ] 2bi [ . ] bi@</ |
1000 van-eck 10 [ head ] [ tail* ] 2bi [ . ] bi@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,211: | Line 1,831: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran"> program VanEck |
||
implicit none |
implicit none |
||
integer eck(1000), i, j |
integer eck(1000), i, j |
||
Line 1,234: | Line 1,854: | ||
write (*,*) |
write (*,*) |
||
end program</ |
end program</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 0 1 0 2 0 2 2 1 6 |
<pre> 0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136</pre> |
4 7 30 25 67 225 488 0 10 136</pre> |
||
=={{header| |
=={{header|Fō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 —i.e. XML, JSON— 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}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,298: | Line 1,892: | ||
fmt.Println("\nTerms 991 to 1000 of the sequence are:") |
fmt.Println("\nTerms 991 to 1000 of the sequence are:") |
||
fmt.Println(a[990:]) |
fmt.Println(a[990:]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,310: | Line 1,904: | ||
Alternatively, using a map to store the latest index of terms previously seen (output as before): |
Alternatively, using a map to store the latest index of terms previously seen (output as before): |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,328: | Line 1,922: | ||
fmt.Println("\nTerms 991 to 1000 of the sequence are:") |
fmt.Println("\nTerms 991 to 1000 of the sequence are:") |
||
fmt.Println(a[990:]) |
fmt.Println(a[990:]) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (elemIndex) |
||
import Data.Maybe (maybe) |
import Data.Maybe (maybe) |
||
Line 1,343: | Line 1,937: | ||
main = do |
main = do |
||
print $ vanEck 10 |
print $ vanEck 10 |
||
print $ drop 990 (vanEck 1000)</ |
print $ drop 990 (vanEck 1000)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[0,0,1,0,2,0,2,2,1,6] |
<pre>[0,0,1,0,2,0,2,2,1,6] |
||
Line 1,350: | Line 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: |
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: |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-} |
||
import qualified Data.Map.Strict as M hiding (drop) |
|||
import Data.List (mapAccumL) |
import Data.List (mapAccumL) |
||
import qualified Data.Map.Strict as M hiding (drop) |
|||
import Data.Maybe (maybe) |
import Data.Maybe (maybe) |
||
--------------------- VAN ECK SEQUENCE ------------------- |
|||
vanEck :: [Int] |
vanEck :: [Int] |
||
Line 1,360: | Line 1,956: | ||
where |
where |
||
go (x, dct) i = |
go (x, dct) i = |
||
((,) =<< (, M.insert x i |
((,) =<< (, M.insert x i dct)) |
||
(maybe 0 (i -) (M.lookup x dct)) |
|||
--------------------------- TEST ------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
mapM_ print $ |
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}} |
{{Out}} |
||
<pre>[0,0,1,0,2,0,2,2,1,6] |
<pre>[0,0,1,0,2,0,2,2,1,6] |
||
Line 1,376: | Line 1,975: | ||
=={{header|J}}== |
=={{header|J}}== |
||
===The tacit verb (function)=== |
===The tacit verb (function)=== |
||
< |
<syntaxhighlight lang="j">VanEck=. (, (<:@:# - }: i: {:))^:(]`0:)</syntaxhighlight> |
||
===The output=== |
===The output=== |
||
< |
<syntaxhighlight lang="j"> VanEck 9 |
||
0 0 1 0 2 0 2 2 1 6 |
0 0 1 0 2 0 2 2 1 6 |
||
990 }. VanEck 999 |
990 }. VanEck 999 |
||
4 7 30 25 67 225 488 0 10 136</ |
4 7 30 25 67 225 488 0 10 136</syntaxhighlight> |
||
===A structured derivation of the verb (function)=== |
===A structured derivation of the verb (function)=== |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
next =. <:@:# - }: i: {: NB. Next term of the sequence |
next =. <:@:# - }: i: {: NB. Next term of the sequence |
||
VanEck=. (, next)^:(]`0:) f. NB. Appending terms and fixing the verb</ |
VanEck=. (, next)^:(]`0:) f. NB. Appending terms and fixing the verb</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Use map to remember last seen index. Computes each value of the sequence in O(1) time. |
Use map to remember last seen index. Computes each value of the sequence in O(1) time. |
||
< |
<syntaxhighlight lang="java"> |
||
import java.util.HashMap; |
import java.util.HashMap; |
||
import java.util.Map; |
import java.util.Map; |
||
Line 1,423: | Line 2,022: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,455: | Line 2,054: | ||
Either declaratively, without premature optimization: |
Either declaratively, without premature optimization: |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,535: | Line 2,134: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>VanEck series: |
<pre>VanEck series: |
||
Line 1,545: | Line 2,144: | ||
or as a map-accumulation, building a look-up table: |
or as a map-accumulation, building a look-up table: |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
|||
// vanEck :: Int -> [Int] |
// vanEck :: Int -> [Int] |
||
Line 1,552: | Line 2,151: | ||
// First n terms of the vanEck series. |
// First n terms of the vanEck series. |
||
[0].concat(mapAccumL( |
[0].concat(mapAccumL( |
||
([x, seen] |
([x, seen]) => i => { |
||
const |
const |
||
prev = seen[x], |
prev = seen[x], |
||
Line 1,558: | Line 2,157: | ||
i - prev |
i - prev |
||
) : 0; |
) : 0; |
||
return [ |
return [ |
||
[v, (seen[x] = i, seen)], v |
[v, (seen[x] = i, seen)], v |
||
]; |
]; |
||
} |
})( |
||
[0, {}] |
|||
)( |
|||
enumFromTo(1)(n - 1) |
|||
)[1]); |
)[1]); |
||
// ----------------------- TEST ------------------------ |
// ----------------------- TEST ------------------------ |
||
const main = () => |
const main = () => |
||
fTable( |
fTable( |
||
"Terms of the VanEck series:\n" |
|||
)( |
|||
n => `${str(n - 10)}-${str(n)}` |
|||
)( |
|||
xs => JSON.stringify(xs.slice(-10)) |
|||
) |
)( |
||
vanEck |
|||
)([10, 1000, 10000]); |
|||
Line 1,580: | Line 2,184: | ||
// enumFromTo :: Int -> Int -> [Int] |
// enumFromTo :: Int -> Int -> [Int] |
||
const enumFromTo = |
const enumFromTo = m => |
||
Array.from({ |
n => Array.from({ |
||
length: 1 + n - m |
length: 1 + n - m |
||
}, (_, i) => m + i); |
}, (_, i) => m + i); |
||
// fTable :: String -> (a -> String) -> (b -> String) -> |
|||
// |
// fTable :: String -> (a -> String) -> |
||
// (b -> String) -> (a -> b) -> [a] -> String |
|||
const fTable = s => |
|||
// Heading -> x display function -> |
// Heading -> x display function -> |
||
// fx display function -> |
// fx display function -> |
||
// f -> values -> tabular string |
// f -> values -> tabular string |
||
xShow => fxShow => f => xs => { |
|||
const |
|||
ys = xs.map(xShow), |
|||
w = Math.max(...ys.map(y => [...y].length)), |
|||
return s + '\n' + zipWith( |
|||
table = zipWith( |
|||
a => b => `${a.padStart(w, " ")} -> ${b}` |
|||
)(ys)( |
|||
xs.map(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, []]); |
|||
// |
// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> |
||
// [x] -> (acc, [y]) |
|||
const mapAccumL = f => |
|||
// A tuple of an accumulation and a list |
|||
// obtained by 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 |
// str :: a -> String |
||
const str = x => x.toString(); |
const str = x => x.toString(); |
||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
||
const zipWith = |
const zipWith = f => |
||
// A list constructed by zipping with a |
|||
const |
|||
// custom function, rather than with the |
|||
lng = Math.min(xs.length, ys.length), |
|||
// default tuple constructor. |
|||
xs => ys => xs.map( |
|||
(x, i) => f(x)(ys[i]) |
|||
).slice( |
|||
0, Math.min(xs.length, ys.length) |
|||
); |
|||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Terms of the VanEck series: |
<pre>Terms of the VanEck series: |
||
Line 1,644: | Line 2,260: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
<lang># Input: an array |
<syntaxhighlight lang="text"># Input: an array |
||
# If the rightmost element, .[-1], does not occur elsewhere, return 0; |
# If the rightmost element, .[-1], does not occur elsewhere, return 0; |
||
# otherwise return the "depth" of its rightmost occurrence in .[0:-2] |
# otherwise return the "depth" of its rightmost occurrence in .[0:-2] |
||
Line 1,663: | Line 2,279: | ||
# The task: |
# The task: |
||
[vanEck(10)], [vanEck(1000)][990:1001] |
[vanEck(10)], [vanEck(1000)][990:1001] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Output === |
=== Output === |
||
<lang>[0,0,1,0,2,0,2,2,1,6] |
<syntaxhighlight lang="text">[0,0,1,0,2,0,2,2,1,6] |
||
[4,7,30,25,67,225,488,0,10,136]</ |
[4,7,30,25,67,225,488,0,10,136]</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function vanecksequence(N, startval=0) |
||
ret = zeros(Int, N) |
ret = zeros(Int, N) |
||
ret[1] = startval |
ret[1] = startval |
||
Line 1,683: | Line 2,299: | ||
println(vanecksequence(10)) |
println(vanecksequence(10)) |
||
println(vanecksequence(1000)[991:1000]) |
println(vanecksequence(1000)[991:1000]) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] |
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] |
||
Line 1,689: | Line 2,305: | ||
</pre> |
</pre> |
||
Alternate version, with a Dict for memoization (output is the same): |
Alternate version, with a Dict for memoization (output is the same): |
||
< |
<syntaxhighlight lang="julia">function vanecksequence(N, startval=0) |
||
ret = zeros(Int, N) |
ret = zeros(Int, N) |
||
ret[1] = startval |
ret[1] = startval |
||
Line 1,701: | Line 2,317: | ||
ret |
ret |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">fun main() { |
||
println("First 10 terms of Van Eck's sequence:") |
println("First 10 terms of Van Eck's sequence:") |
||
vanEck(1, 10) |
vanEck(1, 10) |
||
Line 1,727: | Line 2,343: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 10 terms of Van Eck's sequence: |
<pre>First 10 terms of Van Eck's sequence: |
||
Line 1,754: | Line 2,370: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Return a table of the first n values of the Van Eck sequence |
||
function vanEck (n) |
function vanEck (n) |
||
local seq, foundAt = {0} |
local seq, foundAt = {0} |
||
Line 1,785: | Line 2,401: | ||
local sequence = vanEck(1000) |
local sequence = vanEck(1000) |
||
showValues(sequence, 1, 10) |
showValues(sequence, 1, 10) |
||
showValues(sequence, 991, 1000)</ |
showValues(sequence, 991, 1000)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
4 7 30 25 67 225 488 0 10 136</pre> |
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}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
DIMENSION E(1000) |
DIMENSION E(1000) |
||
E(0)=0 |
E(0)=0 |
||
Line 1,806: | Line 2,489: | ||
S PRINT FORMAT FMT, I, E(I), I+990, E(I+990) |
S PRINT FORMAT FMT, I, E(I), I+990, E(I+990) |
||
VECTOR VALUES FMT = $2HE(,I3,2H)=,I3,S5,2HE(,I3,2H)=,I3*$ |
VECTOR VALUES FMT = $2HE(,I3,2H)=,I3,S5,2HE(,I3,2H)=,I3*$ |
||
END OF PROGRAM </ |
END OF PROGRAM </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>E( 0)= 0 E(990)= 4 |
<pre>E( 0)= 0 E(990)= 4 |
||
Line 1,819: | Line 2,502: | ||
E( 9)= 6 E(999)=136</pre> |
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| |
=={{header|Miranda}}== |
||
<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}} |
{{out}} |
||
<pre>[0,0,1,0,2,0,2,2,1,6] |
|||
<pre> |
|||
[4,7,30,25,67,225,488,0,10,136] |
|||
{4,7,30,25,67,225,488,0,10,136} |
|||
</pre> |
</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}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">const max = 1000 |
||
var a: array[max, int] |
var a: array[max, int] |
||
for n in countup(0, max - 2): |
for n in countup(0, max - 2): |
||
Line 1,843: | Line 2,578: | ||
echo a[..9] |
echo a[..9] |
||
echo "\nTerms 991 to 1000 of the sequence are:" |
echo "\nTerms 991 to 1000 of the sequence are:" |
||
echo a[990..^1]</ |
echo a[990..^1]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,858: | Line 2,593: | ||
Running once through the list of last positions maybe faster |
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 |
[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 |
||
< |
<syntaxhighlight lang="pascal">program VanEck; |
||
{ |
{ |
||
* A: The first term is zero. |
* A: The first term is zero. |
||
Line 1,956: | Line 2,691: | ||
Test(MAXNUM); OutSeen(28); |
Test(MAXNUM); OutSeen(28); |
||
setlength(PosBefore,0); |
setlength(PosBefore,0); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 0 1 0 2 0 2 2 1 6 |
<pre> 0 0 1 0 2 0 2 2 1 6 |
||
Line 1,965: | Line 2,700: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 2,000: | Line 2,735: | ||
Terms 991 through 1000: @{[@seq[990..999]]} |
Terms 991 through 1000: @{[@seq[990..999]]} |
||
END |
END |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:20ex">Van Eck sequence OEIS:A181391; with the first term: 0 |
<pre style="height:20ex">Van Eck sequence OEIS:A181391; with the first term: 0 |
||
Line 2,042: | Line 2,777: | ||
and likewise this can easily create a 32-million-long table in under 2s.<br> |
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. |
While dictionaries are pretty fast, there is a huge overhead adding/updating millions of entries compared to a flat list of int. |
||
<!--< |
<!--<syntaxhighlight lang="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: #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> |
<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,056: | Line 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;">"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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,062: | Line 2,797: | ||
Terms 991 to 1000 of the sequence are:{4,7,30,25,67,225,488,0,10,136} |
Terms 991 to 1000 of the sequence are:{4,7,30,25,67,225,488,0,10,136} |
||
</pre> |
</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}}== |
=={{header|PL/M}}== |
||
< |
<syntaxhighlight lang="plm">100H: |
||
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
||
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; |
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; |
||
Line 2,109: | Line 2,885: | ||
CALL PRINT$SLICE(.ECK(990), 10); |
CALL PRINT$SLICE(.ECK(990), 10); |
||
CALL EXIT; |
CALL EXIT; |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 2,116: | Line 2,892: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">van_eck_init(v(0, 0, _assoc)):- |
||
empty_assoc(_assoc). |
empty_assoc(_assoc). |
||
Line 2,163: | Line 2,939: | ||
write_list(1, 10, Seq), |
write_list(1, 10, Seq), |
||
writeln('Terms 991 to 1000 of the Van Eck sequence:'), |
writeln('Terms 991 to 1000 of the Van Eck sequence:'), |
||
write_list(991, 1000, Seq).</ |
write_list(991, 1000, Seq).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,175: | Line 2,951: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: Using a dict=== |
===Python: Using a dict=== |
||
< |
<syntaxhighlight lang="python">def van_eck(): |
||
n, seen, val = 0, {}, 0 |
n, seen, val = 0, {}, 0 |
||
while True: |
while True: |
||
Line 2,186: | Line 2,962: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
print("Van Eck: first 10 terms: ", list(islice(van_eck(), 10))) |
print("Van Eck: first 10 terms: ", list(islice(van_eck(), 10))) |
||
print("Van Eck: terms 991 - 1000:", list(islice(van_eck(), 1000))[-10:])</ |
print("Van Eck: terms 991 - 1000:", list(islice(van_eck(), 1000))[-10:])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,195: | Line 2,971: | ||
===Python: List based=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="python">def van_eck(): |
||
n = 0 |
n = 0 |
||
seen = [0] |
seen = [0] |
||
Line 2,206: | Line 2,982: | ||
val = 0 |
val = 0 |
||
seen.insert(0, val) |
seen.insert(0, val) |
||
n += 1</ |
n += 1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,216: | Line 2,992: | ||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Van Eck sequence''' |
||
from functools import reduce |
from functools import reduce |
||
Line 2,314: | Line 3,090: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Terms of the Van Eck sequence: |
<pre>Terms of the Van Eck sequence: |
||
Line 2,324: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="python">'''Van Eck series by map-accumulation''' |
||
from functools import reduce |
from functools import reduce |
||
Line 2,402: | Line 3,178: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>The last 10 of the first N vanEck terms: |
<pre>The last 10 of the first N vanEck terms: |
||
Line 2,409: | Line 3,185: | ||
N=1000 -> [4, 7, 30, 25, 67, 225, 488, 0, 10, 136] |
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> |
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}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require racket/stream) |
(require racket/stream) |
||
Line 2,426: | Line 3,219: | ||
"First 10 terms:" (get 0 10 (van-eck)) |
"First 10 terms:" (get 0 10 (van-eck)) |
||
"Terms 991 to 1000 terms:" (get 990 1000 (van-eck)) ; counting from 0</ |
"Terms 991 to 1000 terms:" (get 990 1000 (van-eck)) ; counting from 0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,454: | Line 3,247: | ||
Implemented as lazy, extendable lists. |
Implemented as lazy, extendable lists. |
||
<lang |
<syntaxhighlight lang="raku" line>sub n-van-ecks ($init) { |
||
$init, -> $i, { |
$init, -> $i, { |
||
state %v; |
state %v; |
||
Line 2,486: | Line 3,279: | ||
Terms 991 through 1000: {@seq[990..999]} |
Terms 991 through 1000: {@seq[990..999]} |
||
END |
END |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,532: | Line 3,325: | ||
First 10 terms: 8 0 0 1 0 2 0 2 2 1 |
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> |
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}}== |
=={{header|REXX}}== |
||
===using a list=== |
===using a list=== |
||
This REXX version allows the specification of the ''start'' and ''end'' of the ''Van Eck'' sequence (to be displayed) as |
This REXX version allows the specification of the ''start'' and ''end'' of the ''Van Eck'' sequence (to be displayed) as |
||
<br>well as the initial starting element (the default is zero). |
<br>well as the initial starting element (the default is zero). |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm generates/displays the 'start --> end' elements of the Van Eck sequence.*/ |
||
Parse Arg lo hi d . /*obtain optional arguments from the CL*/ |
|||
If lo=='' | lo==',' Then lo= 1 /*Not specified? Then use the default.*/ |
|||
If hi=='' | hi==',' Then hi= 10 /* " " " " " " */ |
|||
If d=='' | d==',' Then d= 0 /* " " " " " " */ |
|||
dd='' |
|||
$$=; z= $ /*$$: old seq: $: initial value of seq*/ |
|||
z=d /*dd: old seq: d: initial value of seq*/ |
|||
Do i=1 To hi-1 |
|||
end /*HI-1*/ /*REVERSE allows backwards search in $.*/ |
|||
za=wordpos(reverse(z),reverse(dd)) |
|||
/*stick a fork in it, we're all done. */ |
|||
zb=0 |
|||
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' subword($,LO,HI-LO+1)</lang> |
|||
wdd=words(dd) |
|||
Do zz=wdd To 1 By -1 |
|||
If z=word(dd,zz) Then Do |
|||
zb=wdd-zz+1 |
|||
Leave |
|||
End |
|||
End |
|||
z=za |
|||
dd=d |
|||
d=d z |
|||
End /*HI-1*/ /*REVERSE allows backwards search in d.*/ |
|||
Say 'terms ' lo ' through ' hi ' of the Van Eck sequence are: ' subword(d,lo,hi-lo+1) |
|||
Exit |
|||
/*stick a fork in it, we're all done. */</syntaxhighlight> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 2,561: | Line 3,401: | ||
===using a dictionary=== |
===using a dictionary=== |
||
This REXX version (which uses a dictionary) is about '''20,000''' times faster (when using larger numbers) than |
This REXX version (which uses a dictionary) is about '''20,000''' times faster (when using larger numbers) than |
||
<br>using a list (in finding the previous location of an "old" number (term). |
<br>using a list (in finding the previous location of an "old" number (term). |
||
< |
<syntaxhighlight 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.*/ |
|||
If hi=='' | hi==',' Then hi= 10 /* " " " " " " */ |
|||
If sta=='' | sta==',' Then sta= 0 /* " " " " " " */ |
|||
d.0=sta /*d.: the Van Eck sequence as a list.*/ |
|||
x=sta |
|||
do #=1 for HI-1 /*X: is the last term being examined. */ |
|||
a.=. |
|||
if @.x==. then do; @.x= #; $.#= 0; x= 0; end /*new term.*/ |
|||
out='0' |
|||
else do; z= # - @.x; $.#= z; @.x= #; x= z; end /*old term.*/ |
|||
Do n=1 For hi-1 /*X: is the last term being examined. */ |
|||
If a.x==. Then Do /* new term. */ |
|||
a.x=n |
|||
do j=LO to HI-1; out= out $.j /*build a list for the output display. */ |
|||
d.n=0 |
|||
end /*j*/ /*stick a fork in it, we're all done. */ |
|||
x=0 |
|||
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' out</lang> |
|||
End |
|||
Else Do /* old term. */ |
|||
z=n-a.x |
|||
d.n=z |
|||
a.x=n |
|||
x=z |
|||
End |
|||
out=out d.n |
|||
End /*n*/ /*Z: the new term being added To list.*/ |
|||
low=lo-1 |
|||
out=d.low /* initialize the output list */ |
|||
Do j=lo To hi-1 |
|||
out=out d.j /*build a list For the output display. */ |
|||
End /*j*/ |
|||
Say 'terms ' lo ' through ' hi ' of the Van Eck sequence are: ' out |
|||
/*stick a fork in it, we're all done. */</syntaxhighlight> |
|||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
{{out|output|text= 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}}== |
=={{header|Ruby}}== |
||
===Ruby: Using an Array=== |
===Ruby: Using an Array=== |
||
< |
<syntaxhighlight lang="ruby">van_eck = Enumerator.new do |y| |
||
ar = [0] |
ar = [0] |
||
loop do |
loop do |
||
Line 2,591: | Line 3,501: | ||
ve = van_eck.take(1000) |
ve = van_eck.take(1000) |
||
p ve.first(10), ve.last(10) |
p ve.first(10), ve.last(10) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] |
<pre>[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] |
||
Line 2,598: | Line 3,508: | ||
===Ruby: Using a Hash=== |
===Ruby: Using a Hash=== |
||
< |
<syntaxhighlight lang="ruby">class VenEch |
||
include Enumerable |
include Enumerable |
||
Line 2,620: | Line 3,530: | ||
ve = VenEch.new.take(1000) |
ve = VenEch.new.take(1000) |
||
p ve.first(10), ve.last(10) |
p ve.first(10), ve.last(10) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,628: | Line 3,538: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn van_eck_sequence() -> impl std::iter::Iterator<Item = i32> { |
||
let mut index = 0; |
let mut index = 0; |
||
let mut last_term = 0; |
let mut last_term = 0; |
||
Line 2,658: | Line 3,568: | ||
} |
} |
||
println!(); |
println!(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,669: | Line 3,579: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
object VanEck extends App { |
object VanEck extends App { |
||
Line 2,687: | Line 3,597: | ||
println(s"Terms 991 to 1000 are ${vanEck1000.drop(990)}.") |
println(s"Terms 991 to 1000 are ${vanEck1000.drop(990)}.") |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,693: | Line 3,603: | ||
Terms 991 to 1000 are List(4, 7, 30, 25, 67, 225, 488, 0, 10, 136). |
Terms 991 to 1000 are List(4, 7, 30, 25, 67, 225, 488, 0, 10, 136). |
||
</pre> |
</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}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4"> define('eck(n)i,j') :(eck_end) |
||
eck eck = array(n,0) |
eck eck = array(n,0) |
||
i = 0 |
i = 0 |
||
Line 2,712: | Line 3,638: | ||
output = list(ecks, 1, 10) |
output = list(ecks, 1, 10) |
||
output = list(ecks, 991, 1000) |
output = list(ecks, 991, 1000) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 0 1 0 2 0 2 2 1 6 |
<pre>0 0 1 0 2 0 2 2 1 6 |
||
Line 2,718: | Line 3,644: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func van_eck(n) { |
||
var seen = Hash() |
var seen = Hash() |
||
Line 2,734: | Line 3,660: | ||
say van_eck(10) |
say van_eck(10) |
||
say van_eck(1000). |
say van_eck(1000).last(10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,743: | Line 3,669: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Rust}} |
{{trans|Rust}} |
||
< |
<syntaxhighlight lang="swift">struct VanEckSequence: Sequence, IteratorProtocol { |
||
private var index = 0 |
private var index = 0 |
||
private var lastTerm = 0 |
private var lastTerm = 0 |
||
Line 2,771: | Line 3,697: | ||
print(n, terminator: " ") |
print(n, terminator: " ") |
||
} |
} |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,782: | Line 3,708: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">## Mathematically, the first term has index "0", not "1". We do that, also. |
||
set ::vE 0 |
set ::vE 0 |
||
Line 2,805: | Line 3,731: | ||
show vanEck 0 9 |
show vanEck 0 9 |
||
show vanEck 990 999</ |
show vanEck 990 999</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
vanEck(0..9) = 0 0 1 0 2 0 2 2 1 6 |
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 |
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}}== |
=={{header|Visual Basic .NET}}== |
||
{{Trans|C#}} |
{{Trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Imports System.Linq |
||
Module Module1 |
Module Module1 |
||
Dim h() As Integer |
Dim h() As Integer |
||
Line 2,825: | Line 3,763: | ||
Next : a = b : b = c : Next : sho(0) : sho(990) |
Next : a = b : b = c : Next : sho(0) : sho(990) |
||
End Sub |
End Sub |
||
End Module </ |
End Module </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Same as C#. |
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}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="wren">var max = 1000 |
||
var a = List.filled(max, 0) |
var a = List.filled(max, 0) |
||
var seen = {} |
var seen = {} |
||
Line 2,842: | Line 3,839: | ||
System.print(a[0...10]) |
System.print(a[0...10]) |
||
System.print("\nTerms 991 to 1000 of the sequence are:") |
System.print("\nTerms 991 to 1000 of the sequence are:") |
||
System.print(a[990..-1])</ |
System.print(a[990..-1])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,851: | Line 3,848: | ||
Terms 991 to 1000 of the sequence are: |
Terms 991 to 1000 of the sequence are: |
||
[4, 7, 30, 25, 67, 225, 488, 0, 10, 136] |
[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> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="zkl">fcn vanEck(startAt=0){ // --> iterator |
||
(startAt).walker(*).tweak(fcn(n,seen,rprev){ |
(startAt).walker(*).tweak(fcn(n,seen,rprev){ |
||
prev,t := rprev.value, n - seen.find(prev,n); |
prev,t := rprev.value, n - seen.find(prev,n); |
||
Line 2,862: | Line 3,890: | ||
t |
t |
||
}.fp1(Dictionary(),Ref(startAt))).push(startAt) |
}.fp1(Dictionary(),Ref(startAt))).push(startAt) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach n in (9){ |
||
ve:=vanEck(n); |
ve:=vanEck(n); |
||
println("The first ten terms of the Van Eck (%d) sequence are:".fmt(n)); |
println("The first ten terms of the Van Eck (%d) sequence are:".fmt(n)); |
||
Line 2,869: | Line 3,897: | ||
println(" Terms 991 to 1000 of the sequence are:"); |
println(" Terms 991 to 1000 of the sequence are:"); |
||
println("\t",ve.drop(990-10).walk(10).concat(",")); |
println("\t",ve.drop(990-10).walk(10).concat(",")); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:35ex"> |
<pre style="height:35ex"> |
Latest revision as of 07:43, 23 May 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
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.
- References
- Don't Know (the Van Eck Sequence) - Numberphile video.
- Wikipedia Article: Van Eck's Sequence.
- OEIS sequence: A181391.
11l
F van_eck(c)
[Int] r
V n = 0
V seen = [0]
V val = 0
L
r.append(val)
I r.len == c
R r
I val C seen[1..]
val = seen.index(val, 1)
E
val = 0
seen.insert(0, val)
n++
print(‘Van Eck: first 10 terms: ’van_eck(10))
print(‘Van Eck: terms 991 - 1000: ’van_eck(1000)[(len)-10..])
- Output:
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]
8080 Assembly
org 100h
lxi h,ecks ; Zero out 2000 bytes
lxi b,0
lxi d,2000
zero: mov m,b
inx h
dcx d
mov a,d
ora e
jnz zero
lxi b,-1 ; BC = Outer loop variable
outer: inx b
mvi a,3 ; Are we there yet? 1000 = 03E8h
cmp b ; Compare high byte
jnz go
mvi a,0E8h ; Compare low byte
cmp c
jz done
go: mov d,b ; DE = Inner loop variable
mov e,c
inner: dcx d
mov a,d ; <= 0?
ral
jc outer
push b ; Keep both pointers
push d
mov h,b ; Load BC = eck[BC]
mov l,c
call eck
mov c,m
inx h
mov b,m
xchg ; Load HL = -eck[DE]
call eck
xchg
ldax d
cma
mov l,a
inx d
ldax d
cma
mov h,a
inx h ; Two's complement
dad b ; -eck[DE] + eck[BC]
mov a,h ; Unfortunately this does not set flags
ora l ; Check zero
pop d ; Meanwhile, restore the pointers
pop b
jnz inner ; If no match, continue with inner loop
mov h,b ; If we _did_, then get &eck[BC + 1]
mov l,c
inx h
call eck
mov a,c ; Store BC - DE at that address
sub e
mov m,a
inx h
mov a,b
sbb d
mov m,a
jmp outer ; And continue the outer loop
done: lxi h,0 ; Print first 10 terms
call p10
lxi h,990 ; Print last 10 terms
p10: mvi b,10 ; Print 10 terms starting at term HL
call eck
ploop: mov e,m ; Load term into DE
inx h
mov d,m
inx h
push b ; Keep counter
push h ; Keep pointer
xchg ; Term in HL
call printn ; Print term
pop h ; Restore pointer and counter
pop b
dcr b
jnz ploop
lxi d,nl ; Print a newline afterwards
jmp prints
eck: push b ; Set HL = &eck[HL]
lxi b,ecks ; Base address
dad h ; Multiply by two
dad b ; Add base
pop b
ret
printn: lxi d,buf ; Print the number in HL
push d ; Buffer pointer on stack
lxi b,-10 ; Divisor
pdigit: lxi d,-1 ; Quotient
pdiv: inx d
dad b
jc pdiv
mvi a,'0'+10
add l ; Make ASCII digit
pop h
dcx h ; Store digit
mov m,a
push h
xchg
mov a,h ; Quotient nonzero?
ora l
jnz pdigit ; Then there are more digits
pop d ; Otherwise, print string using CP/M
prints: mvi c,9
jmp 5
nl: db 13,10,'$'
db '.....'
buf: db ' $'
ecks: equ $
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
8086 Assembly
LIMIT: equ 1000
cpu 8086
org 100h
section .text
mov di,eck ; Zero out the memory
xor ax,ax
mov cx,LIMIT
rep stosw
mov bx,eck ; Base address
mov cx,LIMIT ; Limit
xor ax,ax
mov si,-1 ; Outer loop index
outer: inc si
dec cx
jcxz done
mov di,si ; Inner loop index
inner: dec di
js outer
shl si,1 ; Shift the loop indices (each entry is 2 bytes)
shl di,1
mov ax,[si+bx] ; Find a match?
cmp ax,[di+bx]
je match
shr si,1 ; If not, shift SI and DI back and keep going
shr di,1
jmp inner
match: mov ax,si ; Calculate the new value
sub ax,di
shr ax,1 ; Compensate for shift
mov [si+bx+2],ax ; Store value
shr si,1 ; Shift SI back and calculate next value
jmp outer
done: xor si,si ; Print first 10 elements
call p10
mov si,LIMIT-10 ; Print last 10 elements⌈
p10: mov cx,10 ; Print 10 elements starting at SI
shl si,1 ; Items are 2 bytes wide
add si,eck
.item: lodsw ; Retrieve item
call printn ; Print it
loop .item
mov dx,nl ; Print a newline afterwards
jmp prints
printn: mov bx,buf ; Print AX
mov bp,10
.digit: xor dx,dx ; Extract digit
div bp
add dl,'0' ; ASCII digit
dec bx
mov [bx],dl ; Store in buffer
test ax,ax ; Any more digits?
jnz .digit
mov dx,bx
prints: mov ah,9 ; Print string in buffer
int 21h
ret
section .data
nl: db 13,10,'$'
db '.....'
buf: db ' $'
section .bss
eck: resw LIMIT
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program vanEckSerie64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
.equ MAXI, 1000
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResultElement: .asciz " @ "
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
TableVanEck: .skip 8 * MAXI
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x2,#0 // begin first element
mov x3,#0 // current counter
ldr x4,qAdrTableVanEck // table address
str x2,[x4,x3,lsl 3] // store first zéro
1: // begin loop
mov x5,x3 // init current indice
2:
sub x5,x5,1 // decrement
cmp x5,0 // end table ?
blt 3f
ldr x6,[x4,x5,lsl 3] // load element
cmp x6,x2 // and compare with the last element
bne 2b // not equal
sub x2,x3,x5 // else compute gap
b 4f
3:
mov x2,#0 // first, move zero to next element
4:
add x3,x3,#1 // increment counter
str x2,[x4,x3,lsl 3] // and store new element
cmp x3,MAXI
blt 1b
mov x2,0
5: // loop display ten elements
ldr x0,[x4,x2,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10 // call décimal conversion
ldr x0,qAdrsMessResultElement
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x1,0 // final zéro
strb w1,[x0,5] //
bl affichageMess // display message
add x2,x2,1 // increment indice
cmp x2,10 // end ?
blt 5b // no -> loop
ldr x0,qAdrszCarriageReturn
bl affichageMess
mov x2,MAXI - 10
6: // loop display ten elements 990-999
ldr x0,[x4,x2,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10 // call décimal conversion
ldr x0,qAdrsMessResultElement
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x1,0 // final zéro
strb w1,[x0,5] //
bl affichageMess // display message
add x2,x2,1 // increment indice
cmp x2,MAXI // end ?
blt 6b // no -> loop
ldr x0,qAdrszCarriageReturn
bl affichageMess
100: // standard end of the program
mov x0, 0 // return code
mov x8, EXIT // request to exit program
svc 0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResultElement: .quad sMessResultElement
qAdrsZoneConv: .quad sZoneConv
qAdrTableVanEck: .quad TableVanEck
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
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 /
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
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
- Output:
Screenshot from Atari 8-bit computer
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
Ada
with Ada.Text_IO;
procedure Van_Eck_Sequence is
Sequence : array (Natural range 1 .. 1_000) of Natural;
procedure Calculate_Sequence is
begin
Sequence (Sequence'First) := 0;
for Index in Sequence'First .. Sequence'Last - 1 loop
Sequence (Index + 1) := 0;
for I in reverse Sequence'First .. Index - 1 loop
if Sequence (I) = Sequence (Index) then
Sequence (Index + 1) := Index - I;
exit;
end if;
end loop;
end loop;
end Calculate_Sequence;
procedure Show (First, Last : in Positive) is
use Ada.Text_IO;
begin
Put ("Element" & First'Image & " .." & Last'Image & " of Van Eck sequence: ");
for I in First .. Last loop
Put (Sequence (I)'Image);
end loop;
New_Line;
end Show;
begin
Calculate_Sequence;
Show (First => 1, Last => 10);
Show (First => 991, Last => 1_000);
end Van_Eck_Sequence;
- Output:
Element 1 .. 10 of Van Eck sequence: 0 0 1 0 2 0 2 2 1 6 Element 991 .. 1000 of Van Eck sequence: 4 7 30 25 67 225 488 0 10 136
ALGOL 68
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 #
# returns the first n elements of the Van Eck sequence #
OP VANECK = ( INT n )[]INT:
BEGIN
[ 1 : IF n < 0 THEN 0 ELSE n FI ]INT result; FOR i TO n DO result[ i ] := 0 OD;
[ 0 : UPB result ]INT pos; FOR i FROM 0 TO n DO pos[ i ] := 0 OD;
FOR i FROM 2 TO n DO
INT j = i - 1;
INT prev = result[ j ];
IF pos[ prev ] /= 0 THEN
# not a new element #
result[ i ] := j - pos[ prev ]
FI;
pos[ prev ] := j
OD;
result
END # VANECK # ;
# construct the first 1000 terms of the sequence #
[]INT seq = VANECK 1000;
# show the first and last 10 elements #
FOR i TO 10 DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD;
print( ( newline ) );
FOR i FROM UPB seq - 9 TO UPB seq DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD;
print( ( newline ) )
END
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
ALGOL-M
begin
integer array eck[1:1000];
integer i, j;
for i := 1 step 1 until 1000 do
eck[i] := 0;
for i := 1 step 1 until 999 do
begin
j := i - 1;
while j > 0 and eck[i] <> eck[j] do
j := j - 1;
if j <> 0 then
eck[i+1] := i - j;
end;
for i := 1 step 1 until 10 do
writeon(eck[i]);
write("");
for i := 991 step 1 until 1000 do
writeon(eck[i]);
end
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
ALGOL W
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 %
% sets s to the first n elements of the Van Eck sequence %
procedure VanEck ( integer array s ( * ) ; integer value n ) ;
begin
integer array pos ( 0 :: n );
for i := 1 until n do s( i ) := 0;
for i := 0 until n do pos( i ) := 0;
for i := 2 until n do begin
integer j, prev;
j := i - 1;
prev := s( j );
if pos( prev ) not = 0 then begin
% not a new element %
s( i ) := j - pos( prev )
end if_pos_prev_ne_0 ;
pos( prev ) := j
end for_j;
end VanEck ;
% construct the first 1000 terms of the sequence %
integer MAX_VAN_ECK;
MAX_VAN_ECK := 1000;
begin
integer array seq ( 1 :: MAX_VAN_ECK );
VanEck( seq, MAX_VAN_ECK );
% show the first and last 10 elements %
for i := 1 until 10 do writeon( i_w := 1, s_w := 0, " ", seq( i ) );
write();
for i := MAX_VAN_ECK - 9 until MAX_VAN_ECK do writeon( i_w := 1, s_w := 0, " ", seq( i ) );
write()
end
end.
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Amazing Hopper
#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
- Output:
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
APL
(10∘↑,[.5]¯10∘↑)(⊢,(⊃∘⌽∊¯1∘↓)∧(1↓⌽)⍳⊃∘⌽)⍣999⊢,0
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
AppleScript
Functional
AppleScript is not the tool for the job, but here is a quick assembly from ready-made parts:
use AppleScript version "2.4"
use scripting additions
-- vanEck :: Int -> [Int]
on vanEck(n)
-- First n terms of the vanEck sequence.
script go
on |λ|(xns, i)
set {x, ns} to xns
set prev to item (1 + x) of ns
if 0 ≠ prev then
set v to i - prev
else
set v to 0
end if
{{v, insert(ns, x, i)}, v}
end |λ|
end script
{0} & item 2 of mapAccumL(go, ¬
{0, replicate(n, 0)}, enumFromTo(1, n - 1))
end vanEck
--------------------------- TEST ---------------------------
on run
unlines({¬
"First 10 terms:", ¬
showList(vanEck(10)), ¬
"", ¬
"Terms 990 to 1000:", ¬
showList(items -10 thru -1 of vanEck(1000))})
end run
------------------------- GENERIC --------------------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
lst
else
{}
end if
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- insert :: [Int] -> Int -> Int -> [Int]
on insert(xs, i, v)
-- A list updated at position i with value v.
set item (1 + i) of xs to v
xs
end insert
-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, delim}
set s to xs as text
set my text item delimiters to dlm
s
end intercalate
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- 'The mapAccumL function behaves like a combination of map and foldl;
-- 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.' (see Hoogle)
-- mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
on mapAccumL(f, acc, xs)
script
on |λ|(a, x, i)
tell mReturn(f) to set pair to |λ|(item 1 of a, x, i)
{item 1 of pair, (item 2 of a) & {item 2 of pair}}
end |λ|
end script
foldl(result, {acc, []}, xs)
end mapAccumL
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> a -> [a]
on replicate(n, a)
set out to {}
if 1 > n then return out
set dbl to {a}
repeat while (1 < n)
if 0 < (n mod 2) then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
-- showList :: [a] -> String
on showList(xs)
"[" & intercalate(", ", map(my str, xs)) & "]"
end showList
-- str :: a -> String
on str(x)
x as string
end str
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
- Output:
First 10 terms: [0, 0, 1, 0, 2, 0, 2, 2, 1, 6] Terms 999 to 1000: [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
Idiomatic
On the contrary, it's right up AppleScript's street.
on vanEckSequence(limit)
script o
property sequence : {}
property lookup : {}
end script
set term to 0
repeat with i from 1 to (limit - 1) -- 1-based indices.
set end of o's sequence to term
set t to term + 1 -- 1-based index.
repeat (t - (count o's lookup)) times
set end of o's lookup to missing value
end repeat
set previous_i to item t of o's lookup
set item t of o's lookup to i
if (previous_i is missing value) then
set term to 0
else
set term to i - previous_i
end if
end repeat
set end of o's sequence to term
return o's sequence
end vanEckSequence
-- Task code:
tell vanEckSequence(1000) to return {items 1 thru 10, items 991 thru 1000}
- Output:
{{0, 0, 1, 0, 2, 0, 2, 2, 1, 6}, {4, 7, 30, 25, 67, 225, 488, 0, 10, 136}}
ARM Assembly
/* ARM assembly Raspberry PI */
/* program vanEckSerie.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ MAXI, 1000
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResultElement: .asciz " @ "
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
TableVanEck: .skip 4 * MAXI
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
mov r2,#0 @ begin first element
mov r3,#0 @ current counter
ldr r4,iAdrTableVanEck @ table address
str r2,[r4,r3,lsl #2] @ store first zéro
1: @ begin loop
mov r5,r3 @ init current indice
2:
sub r5,#1 @ decrement
cmp r5,#0 @ end table ?
movlt r2,#0 @ yes, move zero to next element
blt 3f
ldr r6,[r4,r5,lsl #2] @ load element
cmp r6,r2 @ and compare with the last element
bne 2b @ not equal
sub r2,r3,r5 @ else compute gap
3:
add r3,r3,#1 @ increment counter
str r2,[r4,r3,lsl #2] @ and store new element
cmp r3,#MAXI
blt 1b
mov r2,#0
4: @ loop display ten elements
ldr r0,[r4,r2,lsl #2]
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
ldr r0,iAdrsMessResultElement
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
mov r1,#0 @ final zéro
strb r1,[r0,#5] @
bl affichageMess @ display message
add r2,#1 @ increment indice
cmp r2,#10 @ end ?
blt 4b @ no -> loop
ldr r0,iAdrszCarriageReturn
bl affichageMess
mov r2,#MAXI - 10
5: @ loop display ten elements 990-999
ldr r0,[r4,r2,lsl #2]
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
ldr r0,iAdrsMessResultElement
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
mov r1,#0 @ final zéro
strb r1,[r0,#5] @
bl affichageMess @ display message
add r2,#1 @ increment indice
cmp r2,#MAXI @ end ?
blt 5b @ no -> loop
ldr r0,iAdrszCarriageReturn
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResultElement: .int sMessResultElement
iAdrsZoneConv: .int sZoneConv
iAdrTableVanEck: .int TableVanEck
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Arturo
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
- Output:
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
AWK
# syntax: GAWK -f VAN_ECK_SEQUENCE.AWK
# converted from Go
BEGIN {
limit = 1000
for (i=0; i<limit; i++) {
arr[i] = 0
}
for (n=0; n<limit-1; n++) {
for (m=n-1; m>=0; m--) {
if (arr[m] == arr[n]) {
arr[n+1] = n - m
break
}
}
}
printf("terms 1-10:")
for (i=0; i<10; i++) { printf(" %d",arr[i]) }
printf("\n")
printf("terms 991-1000:")
for (i=990; i<1000; i++) { printf(" %d",arr[i]) }
printf("\n")
exit(0)
}
- Output:
terms 1-10: 0 0 1 0 2 0 2 2 1 6 terms 991-1000: 4 7 30 25 67 225 488 0 10 136
BASIC
10 DEFINT A-Z
20 DIM E(1000)
30 FOR I=0 TO 999
40 FOR J=I-1 TO 0 STEP -1
50 IF E(J)=E(I) THEN E(I+1)=I-J: GOTO 80
60 NEXT J
70 E(I+1)=0
80 NEXT I
90 FOR I=0 TO 9: PRINT E(I);: NEXT
95 PRINT
100 FOR I=990 TO 999: PRINT E(I);: NEXT
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
BASIC256
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
Chipmunk Basic
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
Craft 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
- Output:
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
FreeBASIC
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
- Output:
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
GW-BASIC
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
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
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
XBasic
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
Yabasic
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
BCPL
get "libhdr"
let start() be
$( let eck = vec 999
for i = 0 to 999 do eck!i := 0
for i = 0 to 998 do
for j = i-1 to 0 by -1 do
if eck!i = eck!j then
$( eck!(i+1) := i-j
break
$)
for i = 0 to 9 do writed(eck!i, 4)
wrch('*N')
for i = 990 to 999 do writed(eck!i, 4)
wrch('*N')
$)
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
BQN
EckStep ← ⊢∾(⊑⊑∘⌽∊1↓⌽)◶(0˙)‿((⊑1+⊑⊐˜ 1↓⊢)⌽)
Eck ← {(EckStep⍟(𝕩-1))⥊0}
(Eck 10)≍990↓Eck 1000
- Output:
┌─ ╵ 0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136 ┘
C
#include <stdlib.h>
#include <stdio.h>
int main(int argc, const char *argv[]) {
const int max = 1000;
int *a = malloc(max * sizeof(int));
for (int n = 0; n < max - 1; n ++) {
for (int m = n - 1; m >= 0; m --) {
if (a[m] == a[n]) {
a[n+1] = n - m;
break;
}
}
}
printf("The first ten terms of the Van Eck sequence are:\n");
for (int i = 0; i < 10; i ++) printf("%d ", a[i]);
printf("\n\nTerms 991 to 1000 of the sequence are:\n");
for (int i = 990; i < 1000; i ++) printf("%d ", a[i]);
putchar('\n');
return 0;
}
- Output:
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
C#
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++)
for (d = a, e = b - d, f = h[b]; e <= b; e++)
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); } }
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
C++
#include <iostream>
#include <map>
class van_eck_generator {
public:
int next() {
int result = last_term;
auto iter = last_pos.find(last_term);
int next_term = (iter != last_pos.end()) ? index - iter->second : 0;
last_pos[last_term] = index;
last_term = next_term;
++index;
return result;
}
private:
int index = 0;
int last_term = 0;
std::map<int, int> last_pos;
};
int main() {
van_eck_generator gen;
int i = 0;
std::cout << "First 10 terms of the Van Eck sequence:\n";
for (; i < 10; ++i)
std::cout << gen.next() << ' ';
for (; i < 990; ++i)
gen.next();
std::cout << "\nTerms 991 to 1000 of the sequence:\n";
for (; i < 1000; ++i)
std::cout << gen.next() << ' ';
std::cout << '\n';
return 0;
}
- Output:
First 10 terms of the Van Eck sequence: 0 0 1 0 2 0 2 2 1 6 Terms 991 to 1000 of the sequence: 4 7 30 25 67 225 488 0 10 136
Clojure
(defn van-eck
([] (van-eck 0 0 {}))
([val n seen]
(lazy-seq
(cons val
(let [next (- n (get seen val n))]
(van-eck next
(inc n)
(assoc seen val n)))))))
(println "First 10 terms:" (take 10 (van-eck)))
(println "Terms 991 to 1000 terms:" (take 10 (drop 990 (van-eck))))
- Output:
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)
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
- Output:
0 - 9: 0 0 1 0 2 0 2 2 1 6 990 - 999: 4 7 30 25 67 225 488 0 10 136
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. VAN-ECK.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CALCULATION.
02 ECK PIC 999 OCCURS 1000 TIMES.
02 I PIC 9999.
02 J PIC 9999.
01 OUTPUT-FORMAT.
02 ITEM PIC ZZ9.
02 IDX PIC ZZZ9.
PROCEDURE DIVISION.
B. PERFORM GENERATE-ECK.
PERFORM SHOW VARYING I FROM 1 BY 1 UNTIL I = 11.
PERFORM SHOW VARYING I FROM 991 BY 1 UNTIL I = 1001.
STOP RUN.
SHOW.
MOVE I TO IDX.
MOVE ECK(I) TO ITEM.
DISPLAY 'ECK(' IDX ') = ' ITEM.
GENERATE-ECK SECTION.
B. SET ECK(1) TO 0.
SET I TO 1.
PERFORM GENERATE-TERM
VARYING I FROM 2 BY 1 UNTIL I = 1001.
GENERATE-TERM SECTION.
B. SUBTRACT 2 FROM I GIVING J.
LOOP.
IF J IS LESS THAN 1 GO TO TERM-IS-NEW.
IF ECK(J) = ECK(I - 1) GO TO TERM-IS-OLD.
SUBTRACT 1 FROM J.
GO TO LOOP.
TERM-IS-NEW.
SET ECK(I) TO 0.
GO TO DONE.
TERM-IS-OLD.
COMPUTE ECK(I) = (I - J) - 1.
DONE. EXIT.
- Output:
ECK( 1) = 0 ECK( 2) = 0 ECK( 3) = 1 ECK( 4) = 0 ECK( 5) = 2 ECK( 6) = 0 ECK( 7) = 2 ECK( 8) = 2 ECK( 9) = 1 ECK( 10) = 6 ECK( 991) = 4 ECK( 992) = 7 ECK( 993) = 30 ECK( 994) = 25 ECK( 995) = 67 ECK( 996) = 225 ECK( 997) = 488 ECK( 998) = 0 ECK( 999) = 10 ECK(1000) = 136
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
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Common Lisp
;;Tested using CLISP
(defun VanEck (x) (reverse (VanEckh x 0 0 '(0))))
(defun VanEckh (final index curr lst)
(if (eq index final)
lst
(VanEckh final (+ index 1) (howfar curr lst) (cons curr lst))))
(defun howfar (x lst) (howfarh x lst 0))
(defun howfarh (x lst runningtotal)
(cond
((null lst) 0)
((eq x (car lst)) (+ runningtotal 1))
(t (howfarh x (cdr lst) (+ runningtotal 1)))))
(format t "The first 10 elements are ~a~%" (VanEck 9))
(format t "The 990-1000th elements are ~a~%" (nthcdr 990 (VanEck 999)))
- Output:
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)
(defun van-eck-nm-sequence (n m)
(loop with ac repeat m
for i = (position (car ac) (cdr ac)) do
(push (if i (1+ i) 0) ac)
finally (return (nthcdr (1- n) (nreverse ac)))))
(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))
- Output:
The first 10 elements are: 0 0 1 0 2 0 2 2 1 6 The 991-1000th elements are: 4 7 30 25 67 225 488 0 10 136
Cowgol
include "cowgol.coh";
sub print_list(ptr: [uint16], n: uint8) is
while n > 0 loop
print_i16([ptr]);
print_char(' ');
n := n - 1;
ptr := @next ptr;
end loop;
print_nl();
end sub;
const LIMIT := 1000;
var eck: uint16[LIMIT];
MemZero(&eck as [uint8], @bytesof eck);
var i: @indexof eck;
var j: @indexof eck;
i := 0;
while i < LIMIT-1 loop
j := i-1;
while j != -1 loop
if eck[i] == eck[j] then
eck[i+1] := i-j;
break;
end if;
j := j - 1;
end loop;
i := i + 1;
end loop;
print_list(&eck[0], 10);
print_list(&eck[LIMIT-10], 10);
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
D
import std.stdio;
void vanEck(int firstIndex, int lastIndex) {
int[int] vanEckMap;
int last = 0;
if (firstIndex == 1) {
writefln("VanEck[%d] = %d", 1, 0);
}
for (int n = 2; n <= lastIndex; n++) {
int vanEck = last in vanEckMap ? n - vanEckMap[last] : 0;
vanEckMap[last] = n;
last = vanEck;
if (n >= firstIndex) {
writefln("VanEck[%d] = %d", n, vanEck);
}
}
}
void main() {
writeln("First 10 terms of Van Eck's sequence:");
vanEck(1, 10);
writeln;
writeln("Terms 991 to 1000 of Van Eck's sequence:");
vanEck(991, 1000);
}
- Output:
First 10 terms of Van Eck's sequence: VanEck[1] = 0 VanEck[2] = 0 VanEck[3] = 1 VanEck[4] = 0 VanEck[5] = 2 VanEck[6] = 0 VanEck[7] = 2 VanEck[8] = 2 VanEck[9] = 1 VanEck[10] = 6 Terms 991 to 1000 of Van Eck's sequence: VanEck[991] = 4 VanEck[992] = 7 VanEck[993] = 30 VanEck[994] = 25 VanEck[995] = 67 VanEck[996] = 225 VanEck[997] = 488 VanEck[998] = 0 VanEck[999] = 10 VanEck[1000] = 136
Delphi
See Pascal.
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
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Dyalect
let max = 1000
var a = Array.Empty(max, 0)
for n in 0..(max-2) {
var m = n - 1
while m >= 0 {
if a[m] == a[n] {
a[n+1] = n - m
break
}
m -= 1
}
}
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())")
- Output:
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]
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] & " "
.
F#
The function
// 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)
The Task
- First 50
ecK() |> Seq.take 50 |> Seq.iter(printf "%d "); printfn "";;
- Output:
0 0 1 0 2 0 2 2 1 6 0 5 0 2 6 5 4 0 5 3 0 3 2 9 0 4 9 3 6 14 0 6 3 5 15 0 5 3 5 2 17 0 6 11 0 3 8 0 3 3
- 50 from 991
ecK() |> Seq.skip 990 |> Seq.take 50|> Seq.iter(printf "%d "); printfn "";;
- Output:
4 7 30 25 67 225 488 0 10 136 61 0 4 12 72 0 4 4 1 24 41 385 0 7 22 25 22 2 84 68 282 464 0 10 25 9 151 697 0 6 41 20 257 539 0 6 6 1 29 465
- I thought the longest sequence of non zeroes in the first 100 million items might be interesting
It occurs between 32381749 and 32381774:
9 47 47 1 10 33 27 548 548 1 6 33 6 2 154 15657 695734 270964 235721 238076 4896139 655158 7901804 146089 977945 21475977
Alternative recursive procedure
open System.Collections.Generic
let VanEck() =
let rec _vanEck (num:int) (pos:int) (lastOccurence:Dictionary<int, int>) =
match lastOccurence.TryGetValue num with
| (true, position) ->
set num pos (pos - position) lastOccurence
| _ ->
set num pos 0 lastOccurence
and set num pos next lastOccurenceByNumber = seq {
lastOccurenceByNumber.[num] <- pos
yield next
yield! _vanEck next (pos + 1) lastOccurenceByNumber
}
seq {
yield 0
yield! _vanEck 0 1 (new Dictionary<int, int>())
}
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"
Factor
USING: assocs fry kernel make math namespaces prettyprint
sequences ;
: van-eck ( n -- seq )
[
0 , 1 - H{ } clone '[
building get [ length 1 - ] [ last ] bi _ 3dup
2dup key? [ at - ] [ 3drop 0 ] if , set-at
] times
] { } make ;
1000 van-eck 10 [ head ] [ tail* ] 2bi [ . ] bi@
- Output:
{ 0 0 1 0 2 0 2 2 1 6 } { 4 7 30 25 67 225 488 0 10 136 }
Fortran
program VanEck
implicit none
integer eck(1000), i, j
eck(1) = 0
do 20 i=1, 999
do 10 j=i-1, 1, -1
if (eck(i) .eq. eck(j)) then
eck(i+1) = i-j
go to 20
end if
10 continue
eck(i+1) = 0
20 continue
do 30 i=1, 10
30 write (*,'(I4)',advance='no') eck(i)
write (*,*)
do 40 i=991, 1000
40 write (*,'(I4)',advance='no') eck(i)
write (*,*)
end program
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Fōrmulæ
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
Programs in Fōrmulæ are created/edited online in its website.
In this page you can see and run the program(s) related to this task and their results. You can also change either the programs or the parameters they are called with, for experimentation, but remember that these programs were created with the main purpose of showing a clear solution of the task, and they generally lack any kind of validation.
Solution
Test case
Go
package main
import "fmt"
func main() {
const max = 1000
a := make([]int, max) // all zero by default
for n := 0; n < max-1; n++ {
for m := n - 1; m >= 0; m-- {
if a[m] == a[n] {
a[n+1] = n - m
break
}
}
}
fmt.Println("The first ten terms of the Van Eck sequence are:")
fmt.Println(a[:10])
fmt.Println("\nTerms 991 to 1000 of the sequence are:")
fmt.Println(a[990:])
}
- Output:
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]
Alternatively, using a map to store the latest index of terms previously seen (output as before):
package main
import "fmt"
func main() {
const max = 1000
a := make([]int, max) // all zero by default
seen := make(map[int]int)
for n := 0; n < max-1; n++ {
if m, ok := seen[a[n]]; ok {
a[n+1] = n - m
}
seen[a[n]] = n
}
fmt.Println("The first ten terms of the Van Eck sequence are:")
fmt.Println(a[:10])
fmt.Println("\nTerms 991 to 1000 of the sequence are:")
fmt.Println(a[990:])
}
Haskell
import Data.List (elemIndex)
import Data.Maybe (maybe)
vanEck :: Int -> [Int]
vanEck n = reverse $ iterate go [] !! n
where
go [] = [0]
go xxs@(x:xs) = maybe 0 succ (elemIndex x xs) : xxs
main :: IO ()
main = do
print $ vanEck 10
print $ drop 990 (vanEck 1000)
- Output:
[0,0,1,0,2,0,2,2,1,6] [4,7,30,25,67,225,488,0,10,136]
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:
{-# LANGUAGE TupleSections #-}
import Data.List (mapAccumL)
import qualified Data.Map.Strict as M hiding (drop)
import Data.Maybe (maybe)
--------------------- VAN ECK SEQUENCE -------------------
vanEck :: [Int]
vanEck = 0 : snd (mapAccumL go (0, M.empty) [1 ..])
where
go (x, dct) i =
((,) =<< (, M.insert x i dct))
(maybe 0 (i -) (M.lookup x dct))
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ print $
fmap
((drop . subtract 10) <*> flip take vanEck)
[10, 1000, 10000, 100000, 1000000]
- Output:
[0,0,1,0,2,0,2,2,1,6] [4,7,30,25,67,225,488,0,10,136] [7,43,190,396,2576,3142,0,7,7,1] [92,893,1125,47187,0,7,34,113,140,2984] [8,86,172,8878,172447,0,6,30,874,34143]
J
The tacit verb (function)
VanEck=. (, (<:@:# - }: i: {:))^:(]`0:)
The output
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
A structured derivation of the verb (function)
next =. <:@:# - }: i: {: NB. Next term of the sequence
VanEck=. (, next)^:(]`0:) f. NB. Appending terms and fixing the verb
Java
Use map to remember last seen index. Computes each value of the sequence in O(1) time.
import java.util.HashMap;
import java.util.Map;
public class VanEckSequence {
public static void main(String[] args) {
System.out.println("First 10 terms of Van Eck's sequence:");
vanEck(1, 10);
System.out.println("");
System.out.println("Terms 991 to 1000 of Van Eck's sequence:");
vanEck(991, 1000);
}
private static void vanEck(int firstIndex, int lastIndex) {
Map<Integer,Integer> vanEckMap = new HashMap<>();
int last = 0;
if ( firstIndex == 1 ) {
System.out.printf("VanEck[%d] = %d%n", 1, 0);
}
for ( int n = 2 ; n <= lastIndex ; n++ ) {
int vanEck = vanEckMap.containsKey(last) ? n - vanEckMap.get(last) : 0;
vanEckMap.put(last, n);
last = vanEck;
if ( n >= firstIndex ) {
System.out.printf("VanEck[%d] = %d%n", n, vanEck);
}
}
}
}
- Output:
First 10 terms of Van Eck's sequence: VanEck[1] = 0 VanEck[2] = 0 VanEck[3] = 1 VanEck[4] = 0 VanEck[5] = 2 VanEck[6] = 0 VanEck[7] = 2 VanEck[8] = 2 VanEck[9] = 1 VanEck[10] = 6 Terms 991 to 1000 of Van Eck's sequence: VanEck[991] = 4 VanEck[992] = 7 VanEck[993] = 30 VanEck[994] = 25 VanEck[995] = 67 VanEck[996] = 225 VanEck[997] = 488 VanEck[998] = 0 VanEck[999] = 10 VanEck[1000] = 136
JavaScript
Either declaratively, without premature optimization:
(() => {
'use strict';
// vanEck :: Int -> [Int]
const vanEck = n =>
reverse(
churchNumeral(n)(
xs => 0 < xs.length ? cons(
maybe(
0, succ,
elemIndex(xs[0], xs.slice(1))
),
xs
) : [0]
)([])
);
// TEST -----------------------------------------------
const main = () => {
console.log('VanEck series:\n')
showLog('First 10 terms', vanEck(10))
showLog('Terms 991-1000', vanEck(1000).slice(990))
};
// GENERIC FUNCTIONS ----------------------------------
// Just :: a -> Maybe a
const Just = x => ({
type: 'Maybe',
Nothing: false,
Just: x
});
// Nothing :: Maybe a
const Nothing = () => ({
type: 'Maybe',
Nothing: true,
});
// churchNumeral :: Int -> (a -> a) -> a -> a
const churchNumeral = n => f => x =>
Array.from({
length: n
}, () => f)
.reduce((a, g) => g(a), x)
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs)
// elemIndex :: Eq a => a -> [a] -> Maybe Int
const elemIndex = (x, xs) => {
const i = xs.indexOf(x);
return -1 === i ? (
Nothing()
) : Just(i);
};
// maybe :: b -> (a -> b) -> Maybe a -> b
const maybe = (v, f, m) =>
m.Nothing ? v : f(m.Just);
// reverse :: [a] -> [a]
const reverse = xs =>
'string' !== typeof xs ? (
xs.slice(0).reverse()
) : xs.split('').reverse().join('');
// showLog :: a -> IO ()
const showLog = (...args) =>
console.log(
args
.map(JSON.stringify)
.join(' -> ')
);
// succ :: Int -> Int
const succ = x => 1 + x;
// MAIN ---
return main();
})();
- Output:
VanEck series: "First 10 terms" -> [0,0,1,0,2,0,2,2,1,6] "Terms 991-1000" -> [4,7,30,25,67,225,488,0,10,136]
or as a map-accumulation, building a look-up table:
(() => {
"use strict";
// vanEck :: Int -> [Int]
const vanEck = n =>
// First n terms of the vanEck series.
[0].concat(mapAccumL(
([x, seen]) => i => {
const
prev = seen[x],
v = Boolean(prev) ? (
i - prev
) : 0;
return [
[v, (seen[x] = i, seen)], v
];
})(
[0, {}]
)(
enumFromTo(1)(n - 1)
)[1]);
// ----------------------- TEST ------------------------
const main = () =>
fTable(
"Terms of the VanEck series:\n"
)(
n => `${str(n - 10)}-${str(n)}`
)(
xs => JSON.stringify(xs.slice(-10))
)(
vanEck
)([10, 1000, 10000]);
// ----------------- GENERIC FUNCTIONS -----------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// fTable :: String -> (a -> String) ->
// (b -> String) -> (a -> b) -> [a] -> String
const fTable = s =>
// Heading -> x display function ->
// fx display function ->
// f -> values -> tabular string
xShow => fxShow => f => xs => {
const
ys = xs.map(xShow),
w = Math.max(...ys.map(y => [...y].length)),
table = zipWith(
a => b => `${a.padStart(w, " ")} -> ${b}`
)(ys)(
xs.map(x => fxShow(f(x)))
).join("\n");
return `${s}\n${table}`;
};
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
// [x] -> (acc, [y])
const mapAccumL = f =>
// A tuple of an accumulation and a list
// obtained by 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 =>
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
0, Math.min(xs.length, ys.length)
);
// MAIN ---
return main();
})();
- Output:
Terms of the VanEck series: 0-10 -> [0,0,1,0,2,0,2,2,1,6] 990-1000 -> [4,7,30,25,67,225,488,0,10,136] 9990-10000 -> [7,43,190,396,2576,3142,0,7,7,1]
jq
# 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]
def depth:
.[-1] as $x
| length as $length
| first(range($length-2; -1; -1) as $i
| select(.[$i] == $x) | $length - 1 - $i)
// 0 ;
# Generate a stream of the first $n van Eck integers:
def vanEck($n):
def v:
recurse( if length == $n then empty
else . + [depth] end );
[0] | v | .[-1];
# The task:
[vanEck(10)], [vanEck(1000)][990:1001]
Output
[0,0,1,0,2,0,2,2,1,6]
[4,7,30,25,67,225,488,0,10,136]
Julia
function vanecksequence(N, startval=0)
ret = zeros(Int, N)
ret[1] = startval
for i in 1:N-1
lastseen = findlast(x -> x == ret[i], ret[1:i-1])
if lastseen != nothing
ret[i + 1] = i - lastseen
end
end
ret
end
println(vanecksequence(10))
println(vanecksequence(1000)[991:1000])
- Output:
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
Alternate version, with a Dict for memoization (output is the same):
function vanecksequence(N, startval=0)
ret = zeros(Int, N)
ret[1] = startval
lastseen = Dict{Int, Int}()
for i in 1:N-1
if haskey(lastseen, ret[i])
ret[i + 1] = i - lastseen[ret[i]]
end
lastseen[ret[i]] = i
end
ret
end
Kotlin
fun main() {
println("First 10 terms of Van Eck's sequence:")
vanEck(1, 10)
println("")
println("Terms 991 to 1000 of Van Eck's sequence:")
vanEck(991, 1000)
}
private fun vanEck(firstIndex: Int, lastIndex: Int) {
val vanEckMap = mutableMapOf<Int, Int>()
var last = 0
if (firstIndex == 1) {
println("VanEck[1] = 0")
}
for (n in 2..lastIndex) {
val vanEck = if (vanEckMap.containsKey(last)) n - vanEckMap[last]!! else 0
vanEckMap[last] = n
last = vanEck
if (n >= firstIndex) {
println("VanEck[$n] = $vanEck")
}
}
}
- Output:
First 10 terms of Van Eck's sequence: VanEck[1] = 0 VanEck[2] = 0 VanEck[3] = 1 VanEck[4] = 0 VanEck[5] = 2 VanEck[6] = 0 VanEck[7] = 2 VanEck[8] = 2 VanEck[9] = 1 VanEck[10] = 6 Terms 991 to 1000 of Van Eck's sequence: VanEck[991] = 4 VanEck[992] = 7 VanEck[993] = 30 VanEck[994] = 25 VanEck[995] = 67 VanEck[996] = 225 VanEck[997] = 488 VanEck[998] = 0 VanEck[999] = 10 VanEck[1000] = 136
Lua
-- Return a table of the first n values of the Van Eck sequence
function vanEck (n)
local seq, foundAt = {0}
while #seq < n do
foundAt = nil
for pos = #seq - 1, 1, -1 do
if seq[pos] == seq[#seq] then
foundAt = pos
break
end
end
if foundAt then
table.insert(seq, #seq - foundAt)
else
table.insert(seq, 0)
end
end
return seq
end
-- Show the set of values in table t from key numbers lo to hi
function showValues (t, lo, hi)
for i = lo, hi do
io.write(t[i] .. " ")
end
print()
end
-- Main procedure
local sequence = vanEck(1000)
showValues(sequence, 1, 10)
showValues(sequence, 991, 1000)
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
MACRO-11
.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
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
MAD
NORMAL MODE IS INTEGER
DIMENSION E(1000)
E(0)=0
THROUGH L1, FOR I=0, 1, I.GE.1000
THROUGH L2, FOR J=I-1, -1, J.L.0
WHENEVER E(J).E.E(I)
E(I+1) = I-J
TRANSFER TO L1
END OF CONDITIONAL
L2 CONTINUE
E(I+1)=0
L1 CONTINUE
THROUGH S, FOR I=0, 1, I.GE.10
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
- Output:
E( 0)= 0 E(990)= 4 E( 1)= 0 E(991)= 7 E( 2)= 1 E(992)= 30 E( 3)= 0 E(993)= 25 E( 4)= 2 E(994)= 67 E( 5)= 0 E(995)=225 E( 6)= 2 E(996)=488 E( 7)= 2 E(997)= 0 E( 8)= 1 E(998)= 10 E( 9)= 6 E(999)=136
Mathematica /Wolfram Language
TakeList[Nest[If[MemberQ[#//Most, #//Last], Join[#, Length[#] - Last@Position[#//Most, #//Last]], Append[#, 0]]&, {0}, 999], {10, -10}] // Column
- Output:
{0,0,1,0,2,0,2,2,1,6} {4,7,30,25,67,225,488,0,10,136}
Miranda
main :: [sys_message]
main = [ Stdout (show list ++ "\n")
| list <- [take 10 eck, take 10 (drop 990 eck)]
]
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
- Output:
[0,0,1,0,2,0,2,2,1,6] [4,7,30,25,67,225,488,0,10,136]
Modula-2
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.
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Nim
const max = 1000
var a: array[max, int]
for n in countup(0, max - 2):
for m in countdown(n - 1, 0):
if a[m] == a[n]:
a[n + 1] = n - m
break
echo "The first ten terms of the Van Eck sequence are:"
echo a[..9]
echo "\nTerms 991 to 1000 of the sequence are:"
echo a[990..^1]
- Output:
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]
Pascal
I memorize the last position of each number that occured and use a circular buffer to remember last values. Running once through the list of last positions maybe faster Try it online! takes only 1.4 s for 32,381,775
program VanEck;
{
* 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 previousely.}
uses
sysutils;
const
MAXNUM = 32381775;//1000*1000*1000;
MAXSEENIDX = (1 shl 7)-1;
var
PosBefore : array of UInt32;
LastSeen : array[0..MAXSEENIDX]of UInt32;// circular buffer
SeenIdx,HaveSeen : Uint32;
procedure OutSeen(Cnt:NativeInt);
var
I,S_Idx : NativeInt;
Begin
IF Cnt > MAXSEENIDX then
Cnt := MAXSEENIDX;
If Cnt > HaveSeen then
Cnt := HaveSeen;
S_Idx := SeenIdx;
S_Idx := (S_Idx-Cnt);
IF S_Idx < 0 then
inc(S_Idx,MAXSEENIDX);
For i := 1 to Cnt do
Begin
write(' ',LastSeen[S_Idx]);
S_Idx:= (S_Idx+1) AND MAXSEENIDX;
end;
writeln;
end;
procedure Test(MaxTestCnt: Uint32);
var
i, actnum, Posi, S_Idx: Uint32;
{$IFDEF FPC}
pPosBef, pSeen: pUint32;
{$ELSE}
pPosBef, pSeen: array of UInt32;
{$ENDIF}
begin
HaveSeen := 0;
if MaxTestCnt > MAXNUM then
EXIT;
Fillchar(LastSeen, SizeOf(LastSeen), #0);
//setlength and clear
setlength(PosBefore, 0);
setlength(PosBefore, MaxTestCnt);
{$IFDEF FPC}
pPosBef := @PosBefore[0];
pSeen := @LastSeen[0];
{$ELSE}
SetLength(pSeen, SizeOf(LastSeen));
setlength(pPosBef, MaxTestCnt);
move(PosBefore[0], pPosBef[0], length(pPosBef));
move(LastSeen[0], pSeen[0], length(pSeen));
{$ENDIF}
S_Idx := 0;
i := 1;
actnum := 0;
repeat
// save value
pSeen[S_Idx] := actnum;
S_Idx := (S_Idx + 1) and MAXSEENIDX;
//examine new value often out of cache
Posi := pPosBef[actnum];
pPosBef[actnum] := i;
// if Posi=0 ? actnum = 0:actnum = i-Posi
if Posi = 0 then
actnum := 0
else
actnum := i - Posi;
inc(i);
until i > MaxTestCnt;
HaveSeen := i - 1;
SeenIdx := S_Idx;
{$IFNDEF FPC}
move(pPosBef[0], PosBefore[0], length(pPosBef));
move(pSeen[0], LastSeen[0], length(pSeen));
{$ENDIF}
end;
Begin
Test(10) ; OutSeen(10000);
Test(1000); OutSeen(10);
Test(MAXNUM); OutSeen(28);
setlength(PosBefore,0);
end.
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136 0 9 47 47 1 10 33 27 548 548 1 6 33 6 2 154 15657 695734 270964 235721 238076 4896139 655158 7901804 146089 977945 21475977 0
Perl
use strict;
use warnings;
use feature 'say';
sub van_eck {
my($init,$max) = @_;
my(%v,$k);
my @V = my $i = $init;
for (1..$max) {
$k++;
my $t = $v{$i} ? $k - $v{$i} : 0;
$v{$i} = $k;
push @V, $i = $t;
}
@V;
}
for (
['A181391', 0],
['A171911', 1],
['A171912', 2],
['A171913', 3],
['A171914', 4],
['A171915', 5],
['A171916', 6],
['A171917', 7],
['A171918', 8],
) {
my($seq, $start) = @$_;
my @seq = van_eck($start,1000);
say <<~"END";
Van Eck sequence OEIS:$seq; with the first term: $start
First 10 terms: @{[@seq[0 .. 9]]}
Terms 991 through 1000: @{[@seq[990..999]]}
END
}
- Output:
Van Eck sequence OEIS:A181391; with the first term: 0 First 10 terms: 0 0 1 0 2 0 2 2 1 6 Terms 991 through 1000: 4 7 30 25 67 225 488 0 10 136 Van Eck sequence OEIS:A171911; with the first term: 1 First 10 terms: 1 0 0 1 3 0 3 2 0 3 Terms 991 through 1000: 0 6 53 114 302 0 5 9 22 71 Van Eck sequence OEIS:A171912; with the first term: 2 First 10 terms: 2 0 0 1 0 2 5 0 3 0 Terms 991 through 1000: 8 92 186 0 5 19 41 413 0 5 Van Eck sequence OEIS:A171913; with the first term: 3 First 10 terms: 3 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 5 5 1 17 192 0 6 34 38 179 Van Eck sequence OEIS:A171914; with the first term: 4 First 10 terms: 4 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 33 410 0 6 149 0 3 267 0 3 Van Eck sequence OEIS:A171915; with the first term: 5 First 10 terms: 5 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 60 459 0 7 13 243 0 4 10 211 Van Eck sequence OEIS:A171916; with the first term: 6 First 10 terms: 6 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 6 19 11 59 292 0 6 6 1 12 Van Eck sequence OEIS:A171917; with the first term: 7 First 10 terms: 7 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 11 7 2 7 2 2 1 34 24 238 Van Eck sequence OEIS:A171918; with the first term: 8 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
Phix
Just like the pascal entry, instead of searching/dictionaries use a fast direct/parallel lookup table,
and likewise this can easily create a 32-million-long table in under 2s.
While dictionaries are pretty fast, there is a huge overhead adding/updating millions of entries compared to a flat list of int.
constant lim = 1000 sequence van_eck = repeat(0,lim), pos_before = repeat(0,lim) for n=1 to lim-1 do integer vn = van_eck[n]+1, prev = pos_before[vn] if prev!=0 then van_eck[n+1] = n - prev end if pos_before[vn] = n end for printf(1,"The first ten terms of the Van Eck sequence are:%v\n",{van_eck[1..10]}) printf(1,"Terms 991 to 1000 of the sequence are:%V\n",{van_eck[991..1000]})
- Output:
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}
Picat
Note: Picat is 1-based.
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.
- Output:
{0,0,1,0,2,0,2,2,1,6} {4,7,30,25,67,225,488,0,10,136}
Recursive
Same idea but recursive.
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).
- Output:
{0,0,1,0,2,0,2,2,1,6,0} {4,7,30,25,67,225,488,0,10,136}
PL/M
100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (7) BYTE INITIAL ('..... $');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
PRINT$SLICE: PROCEDURE (LIST, N);
DECLARE (I, N, LIST, L BASED LIST) ADDRESS;
DO I=0 TO N-1;
CALL PRINT$NUMBER(L(I));
END;
CALL PRINT(.(13,10,'$'));
END PRINT$SLICE;
DECLARE ECK (1000) ADDRESS;
DECLARE (I, J) ADDRESS;
ECK(0) = 0;
DO I=0 TO LAST(ECK)-1;
J = I - 1;
DO WHILE J <> 0FFFFH; /* WHAT IS SIGNED MATH */
IF ECK(I) = ECK(J) THEN DO;
ECK(I+1) = I-J;
GO TO NEXT;
END;
J = J - 1;
END;
ECK(I+1) = 0;
NEXT:
END;
CALL PRINT$SLICE(.ECK(0), 10);
CALL PRINT$SLICE(.ECK(990), 10);
CALL EXIT;
EOF
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Prolog
van_eck_init(v(0, 0, _assoc)):-
empty_assoc(_assoc).
van_eck_next(v(Index, Last_term, Last_pos), v(Index1, Next_term, Last_pos1)):-
(get_assoc(Last_term, Last_pos, V) ->
Next_term is Index - V
;
Next_term = 0
),
Index1 is Index + 1,
put_assoc(Last_term, Last_pos, Index, Last_pos1).
van_eck_sequence(N, Seq):-
van_eck_init(V),
van_eck_sequence(N, V, Seq).
van_eck_sequence(0, _, []):-!.
van_eck_sequence(N, V, [Term|Rest]):-
V = v(_, Term, _),
van_eck_next(V, V1),
N1 is N - 1,
van_eck_sequence(N1, V1, Rest).
write_list(From, To, _, _):-
To < From,
!.
write_list(_, _, _, []):-!.
write_list(From, To, N, [_|Rest]):-
From > N,
!,
N1 is N + 1,
write_list(From, To, N1, Rest).
write_list(From, To, N, [E|Rest]):-
writef('%t ', [E]),
F1 is From + 1,
N1 is N + 1,
write_list(F1, To, N1, Rest).
write_list(From, To, List):-
write_list(From, To, 1, List),
nl.
main:-
van_eck_sequence(1000, Seq),
writeln('First 10 terms of the Van Eck sequence:'),
write_list(1, 10, Seq),
writeln('Terms 991 to 1000 of the Van Eck sequence:'),
write_list(991, 1000, Seq).
- Output:
First 10 terms of the Van Eck sequence: 0 0 1 0 2 0 2 2 1 6 Terms 991 to 1000 of the Van Eck sequence: 4 7 30 25 67 225 488 0 10 136
Python
Python: Using a dict
def van_eck():
n, seen, val = 0, {}, 0
while True:
yield val
last = {val: n}
val = n - seen.get(val, n)
seen.update(last)
n += 1
#%%
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:])
- Output:
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]
Python: List based
The following alternative stores the sequence so far in a list seen
rather than the first example that just stores last occurrences in a dict.
def van_eck():
n = 0
seen = [0]
val = 0
while True:
yield val
if val in seen[1:]:
val = seen.index(val, 1)
else:
val = 0
seen.insert(0, val)
n += 1
- Output:
As before.
Python: Composition of pure functions
As an alternative to the use of generators, a declarative definition in terms of a Church numeral function:
'''Van Eck sequence'''
from functools import reduce
from itertools import repeat
# vanEck :: Int -> [Int]
def vanEck(n):
'''First n terms of the van Eck sequence.'''
return churchNumeral(n)(
lambda xs: cons(
maybe(0)(succ)(
elemIndex(xs[0])(xs[1:])
)
)(xs) if xs else [0]
)([])[::-1]
# TEST ----------------------------------------------------
def main():
'''Terms of the Van Eck sequence'''
print(
main.__doc__ + ':\n\n' +
'First 10: '.rjust(18, ' ') + repr(vanEck(10)) + '\n' +
'991 - 1000: '.rjust(18, ' ') + repr(vanEck(1000)[990:])
)
# GENERIC -------------------------------------------------
# Just :: a -> Maybe a
def Just(x):
'''Constructor for an inhabited Maybe (option type) value.
Wrapper containing the result of a computation.
'''
return {'type': 'Maybe', 'Nothing': False, 'Just': x}
# Nothing :: Maybe a
def Nothing():
'''Constructor for an empty Maybe (option type) value.
Empty wrapper returned where a computation is not possible.
'''
return {'type': 'Maybe', 'Nothing': True}
# churchNumeral :: Int -> (a -> a) -> a -> a
def churchNumeral(n):
'''n applications of a function
'''
return lambda f: lambda x: reduce(
lambda a, g: g(a), repeat(f, n), x
)
# cons :: a -> [a] -> [a]
def cons(x):
'''Construction of a list from a head and a tail.
'''
return lambda xs: [x] + xs
# elemIndex :: Eq a => a -> [a] -> Maybe Int
def elemIndex(x):
'''Just the index of the first element in xs
which is equal to x,
or Nothing if there is no such element.
'''
def go(xs):
try:
return Just(xs.index(x))
except ValueError:
return Nothing()
return go
# maybe :: b -> (a -> b) -> Maybe a -> b
def maybe(v):
'''Either the default value v, if m is Nothing,
or the application of f to x,
where m is Just(x).
'''
return lambda f: lambda m: v if None is m or m.get('Nothing') else (
f(m.get('Just'))
)
# succ :: Enum a => a -> a
def succ(x):
'''The successor of a value.
For numeric types, (1 +).
'''
return 1 + x
# MAIN ---
if __name__ == '__main__':
main()
- Output:
Terms of the Van Eck sequence: First 10: [0, 0, 1, 0, 2, 0, 2, 2, 1, 6] 991 - 1000: [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
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.
'''Van Eck series by map-accumulation'''
from functools import reduce
from itertools import repeat
# vanEck :: Int -> [Int]
def vanEck(n):
'''First n terms of the vanEck sequence.'''
def go(xns, i):
x, ns = xns
prev = ns[x]
v = i - prev if 0 is not prev else 0
return (
(v, insert(ns, x, i)),
v
)
return [0] + mapAccumL(go)((0, list(repeat(0, n))))(
range(1, n)
)[1]
# -------------------------- TEST --------------------------
# main :: IO ()
def main():
'''The last 10 of the first N vanEck terms'''
print(
fTable(main.__doc__ + ':\n')(
lambda m: 'N=' + str(m), repr,
lambda n: vanEck(n)[-10:], [10, 1000, 10000]
)
)
# ----------------------- FORMATTING -----------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return go
# ------------------------ GENERIC -------------------------
# insert :: Array Int -> Int -> Int -> Array Int
def insert(xs, i, v):
'''An array updated at position i with value v.'''
xs[i] = v
return xs
# mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
def mapAccumL(f):
'''A tuple of an accumulation and a list derived by a
combined map and fold,
with accumulation from left to right.
'''
def go(a, x):
tpl = f(a[0], x)
return (tpl[0], a[1] + [tpl[1]])
return lambda acc: lambda xs: (
reduce(go, xs, (acc, []))
)
# MAIN ---
if __name__ == '__main__':
main()
- Output:
The last 10 of the first N vanEck terms: N=10 -> [0, 0, 1, 0, 2, 0, 2, 2, 1, 6] N=1000 -> [4, 7, 30, 25, 67, 225, 488, 0, 10, 136] N=10000 -> [7, 43, 190, 396, 2576, 3142, 0, 7, 7, 1]
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
- Output:
[ 0 0 1 0 2 0 2 2 1 6 ] [ 4 7 30 25 67 225 488 0 10 136 ]
Racket
#lang racket
(require racket/stream)
(define (van-eck)
(define (next val n seen)
(define val1 (- n (hash-ref seen val n)))
(stream-cons val (next val1 (+ n 1) (hash-set seen val n))))
(next 0 0 (hash)))
(define (get m n s)
(stream->list
(stream-take (stream-tail s m)
(- n m))))
"First 10 terms:" (get 0 10 (van-eck))
"Terms 991 to 1000 terms:" (get 990 1000 (van-eck)) ; counting from 0
- Output:
"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)
Raku
(formerly Perl 6) There is not a Van Eck sequence, rather a series of related sequences that differ in their starting value. This task is nominally for the sequence starting with the value 0. This Raku implementation will handle any integer starting value.
Specifically handles:
- OEIS:A181391 - Van Eck sequence starting with 0
- OEIS:A171911 - Van Eck sequence starting with 1
- OEIS:A171912 - Van Eck sequence starting with 2
- OEIS:A171913 - Van Eck sequence starting with 3
- OEIS:A171914 - Van Eck sequence starting with 4
- OEIS:A171915 - Van Eck sequence starting with 5
- OEIS:A171916 - Van Eck sequence starting with 6
- OEIS:A171917 - Van Eck sequence starting with 7
- OEIS:A171918 - Van Eck sequence starting with 8
among others.
Implemented as lazy, extendable lists.
sub n-van-ecks ($init) {
$init, -> $i, {
state %v;
state $k;
$k++;
my $t = %v{$i}.defined ?? $k - %v{$i} !! 0;
%v{$i} = $k;
$t
} ... *
}
for <
A181391 0
A171911 1
A171912 2
A171913 3
A171914 4
A171915 5
A171916 6
A171917 7
A171918 8
> -> $seq, $start {
my @seq = n-van-ecks($start);
# The task
put qq:to/END/
Van Eck sequence OEIS:$seq; with the first term: $start
First 10 terms: {@seq[^10]}
Terms 991 through 1000: {@seq[990..999]}
END
}
- Output:
Van Eck sequence OEIS:A181391; with the first term: 0 First 10 terms: 0 0 1 0 2 0 2 2 1 6 Terms 991 through 1000: 4 7 30 25 67 225 488 0 10 136 Van Eck sequence OEIS:A171911; with the first term: 1 First 10 terms: 1 0 0 1 3 0 3 2 0 3 Terms 991 through 1000: 0 6 53 114 302 0 5 9 22 71 Van Eck sequence OEIS:A171912; with the first term: 2 First 10 terms: 2 0 0 1 0 2 5 0 3 0 Terms 991 through 1000: 8 92 186 0 5 19 41 413 0 5 Van Eck sequence OEIS:A171913; with the first term: 3 First 10 terms: 3 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 5 5 1 17 192 0 6 34 38 179 Van Eck sequence OEIS:A171914; with the first term: 4 First 10 terms: 4 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 33 410 0 6 149 0 3 267 0 3 Van Eck sequence OEIS:A171915; with the first term: 5 First 10 terms: 5 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 60 459 0 7 13 243 0 4 10 211 Van Eck sequence OEIS:A171916; with the first term: 6 First 10 terms: 6 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 6 19 11 59 292 0 6 6 1 12 Van Eck sequence OEIS:A171917; with the first term: 7 First 10 terms: 7 0 0 1 0 2 0 2 2 1 Terms 991 through 1000: 11 7 2 7 2 2 1 34 24 238 Van Eck sequence OEIS:A171918; with the first term: 8 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
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;
};
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
REXX
using a list
This REXX version allows the specification of the start and end of the Van Eck sequence (to be displayed) as
well as the initial starting element (the default is zero).
/*REXX pgm generates/displays the 'start --> end' elements of the Van Eck sequence.*/
Parse Arg lo hi d . /*obtain optional arguments from the CL*/
If lo=='' | lo==',' Then lo= 1 /*Not specified? Then use the default.*/
If hi=='' | hi==',' Then hi= 10 /* " " " " " " */
If d=='' | d==',' Then d= 0 /* " " " " " " */
dd=''
z=d /*dd: old seq: d: initial value of seq*/
Do i=1 To hi-1
za=wordpos(reverse(z),reverse(dd))
zb=0
wdd=words(dd)
Do zz=wdd To 1 By -1
If z=word(dd,zz) Then Do
zb=wdd-zz+1
Leave
End
End
z=za
dd=d
d=d z
End /*HI-1*/ /*REVERSE allows backwards search in d.*/
Say 'terms ' lo ' through ' hi ' of the Van Eck sequence are: ' subword(d,lo,hi-lo+1)
Exit
/*stick a fork in it, we're all done. */
- output when using the default inputs:
terms 1 through 10 of the Van Eck sequence are: 0 0 1 0 2 0 2 2 1 6
- output when using the inputs of: 991 1000
terms 991 through 1000 of the Van Eck sequence are: 4 7 30 25 67 225 488 0 10 136
- output when using the inputs of: 1 20 6
terms 1 through 20 of the Van Eck sequence are: 6 0 0 1 0 2 0 2 2 1 6 10 0 6 3 0 3 2 9 0
using a dictionary
This REXX version (which uses a dictionary) is about 20,000 times faster (when using larger numbers) than
using a list (in finding the previous location of an "old" number (term).
/*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.*/
If hi=='' | hi==',' Then hi= 10 /* " " " " " " */
If sta=='' | sta==',' Then sta= 0 /* " " " " " " */
d.0=sta /*d.: the Van Eck sequence as a list.*/
x=sta
a.=.
out='0'
Do n=1 For hi-1 /*X: is the last term being examined. */
If a.x==. Then Do /* new term. */
a.x=n
d.n=0
x=0
End
Else Do /* old term. */
z=n-a.x
d.n=z
a.x=n
x=z
End
out=out d.n
End /*n*/ /*Z: the new term being added To list.*/
low=lo-1
out=d.low /* initialize the output list */
Do j=lo To hi-1
out=out d.j /*build a list For the output display. */
End /*j*/
Say 'terms ' lo ' through ' hi ' of the Van Eck sequence are: ' out
/*stick a fork in it, we're all done. */
- output is identical to the 1st REXX version.
RPL
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 |
- Input:
10 VANECK 1000 VANECK 991 LASTL
- Output:
2: { 0 0 1 0 2 0 2 2 1 6 } 1: { 4 7 30 25 67 225 488 0 10 136 }
Ruby
Ruby: Using an Array
van_eck = Enumerator.new do |y|
ar = [0]
loop do
y << (term = ar.last) # yield
ar << (ar.count(term)==1 ? 0 : ar.size - 1 - ar[0..-2].rindex(term))
end
end
ve = van_eck.take(1000)
p ve.first(10), ve.last(10)
- Output:
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
Ruby: Using a Hash
class VenEch
include Enumerable
def initialize()
@i = 0;
@val = 0;
@seen = {};
end
def add_num num
@val = @i - @seen.fetch(num, @i)
@seen[num] = @i
@i += 1
end
def each(&block)
loop { block.call(@val); add_num @val }
end
end
ve = VenEch.new.take(1000)
p ve.first(10), ve.last(10)
- Output:
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
Rust
fn van_eck_sequence() -> impl std::iter::Iterator<Item = i32> {
let mut index = 0;
let mut last_term = 0;
let mut last_pos = std::collections::HashMap::new();
std::iter::from_fn(move || {
let result = last_term;
let mut next_term = 0;
if let Some(v) = last_pos.get_mut(&last_term) {
next_term = index - *v;
*v = index;
} else {
last_pos.insert(last_term, index);
}
last_term = next_term;
index += 1;
Some(result)
})
}
fn main() {
let mut v = van_eck_sequence().take(1000);
println!("First 10 terms of the Van Eck sequence:");
for n in v.by_ref().take(10) {
print!("{} ", n);
}
println!("\nTerms 991 to 1000 of the Van Eck sequence:");
for n in v.skip(980) {
print!("{} ", n);
}
println!();
}
- Output:
First 10 terms of the Van Eck sequence: 0 0 1 0 2 0 2 2 1 6 Terms 991 to 1000 of the Van Eck sequence: 4 7 30 25 67 225 488 0 10 136
Scala
object VanEck extends App {
def vanEck(n: Int): List[Int] = {
def vanEck(values: List[Int]): List[Int] =
if (values.size < n)
vanEck(math.max(0, values.indexOf(values.head, 1)) :: values)
else
values
vanEck(List(0)).reverse
}
val vanEck1000 = vanEck(1000)
println(s"The first 10 terms are ${vanEck1000.take(10)}.")
println(s"Terms 991 to 1000 are ${vanEck1000.drop(990)}.")
}
- Output:
The first 10 terms are List(0, 0, 1, 0, 2, 0, 2, 2, 1, 6). Terms 991 to 1000 are List(4, 7, 30, 25, 67, 225, 488, 0, 10, 136).
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;
- Output:
[0 0 1 0 2 0 2 2 1 6] [4 7 30 25 67 225 488 0 10 136]
SNOBOL4
define('eck(n)i,j') :(eck_end)
eck eck = array(n,0)
i = 0
eouter i = lt(i,n - 1) i + 1 :f(return)
j = i
einner j = gt(j,0) j - 1 :f(eouter)
eck<i + 1> = eq(eck<i>,eck<j>) i - j :s(eouter)f(einner)
eck_end
define('list(arr,start,stop)') :(list_end)
list list = list arr<start> ' '
start = lt(start,stop) start + 1 :s(list)f(return)
list_end
ecks = eck(1000)
output = list(ecks, 1, 10)
output = list(ecks, 991, 1000)
end
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Sidef
func van_eck(n) {
var seen = Hash()
var seq = [0]
var prev = seq[-1]
for k in (1 ..^ n) {
seq << (seen.has(prev) ? (k - seen{prev}) : 0)
seen{prev} = k
prev = seq[-1]
}
seq
}
say van_eck(10)
say van_eck(1000).last(10)
- Output:
[0, 0, 1, 0, 2, 0, 2, 2, 1, 6] [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]
Swift
struct VanEckSequence: Sequence, IteratorProtocol {
private var index = 0
private var lastTerm = 0
private var lastPos = Dictionary<Int, Int>()
mutating func next() -> Int? {
let result = lastTerm
var nextTerm = 0
if let v = lastPos[lastTerm] {
nextTerm = index - v
}
lastPos[lastTerm] = index
lastTerm = nextTerm
index += 1
return result
}
}
let seq = VanEckSequence().prefix(1000)
print("First 10 terms of the Van Eck sequence:")
for n in seq.prefix(10) {
print(n, terminator: " ")
}
print("\nTerms 991 to 1000 of the Van Eck sequence:")
for n in seq.dropFirst(990) {
print(n, terminator: " ")
}
print()
- Output:
First 10 terms of the Van Eck sequence: 0 0 1 0 2 0 2 2 1 6 Terms 991 to 1000 of the Van Eck sequence: 4 7 30 25 67 225 488 0 10 136
Tcl
## Mathematically, the first term has index "0", not "1". We do that, also.
set ::vE 0
proc vanEck {n} {
global vE vEocc
while {$n >= [set k [expr {[llength $vE] - 1}]]} {
set kv [lindex $vE $k]
## value $kv @ $k is not yet stuffed into vEocc()
lappend vE [expr {[info exists vEocc($kv)] ? $k - $vEocc($kv) : 0}]
set vEocc($kv) $k
}
return [lindex $vE $n]
}
proc show {func from to} {
for {set n $from} {$n <= $to} {incr n} {
append r " " [$func $n]
}
puts "${func}($from..$to) =$r"
}
show vanEck 0 9
show vanEck 990 999
- Output:
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
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.
- Output:
[0 0 1 0 2 0 2 2 1 6] [4 7 30 25 67 225 488 0 10 136]
Visual Basic .NET
Imports System.Linq
Module Module1
Dim h() As Integer
Sub sho(i As Integer)
Console.WriteLine(String.Join(" ", h.Skip(i).Take(10)))
End Sub
Sub Main()
Dim a, b, c, d, f, g As Integer : g = 1000
h = new Integer(g){} : a = 0 : b = 1 : For c = 2 To g
f = h(b) : For d = a To 0 Step -1
If f = h(d) Then h(c) = b - d: Exit For
Next : a = b : b = c : Next : sho(0) : sho(990)
End Sub
End Module
- Output:
Same as C#.
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..])
}
- Output:
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]
VTL-2
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
- Output:
0 0 1 0 2 0 2 2 1 6 4 7 30 25 67 225 488 0 10 136
Wren
var max = 1000
var a = List.filled(max, 0)
var seen = {}
for (n in 0...max-1) {
var m = seen[a[n]]
if (m != null) a[n+1] = n - m
seen[a[n]] = n
}
System.print("The first ten terms of the Van Eck sequence are:")
System.print(a[0...10])
System.print("\nTerms 991 to 1000 of the sequence are:")
System.print(a[990..-1])
- Output:
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]
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, ", ");
];
];
]
- Output:
0, 0, 1, 0, 2, 0, 2, 2, 1, 6 4, 7, 30, 25, 67, 225, 488, 0, 10, 136
zkl
fcn vanEck(startAt=0){ // --> iterator
(startAt).walker(*).tweak(fcn(n,seen,rprev){
prev,t := rprev.value, n - seen.find(prev,n);
seen[prev] = n;
rprev.set(t);
t
}.fp1(Dictionary(),Ref(startAt))).push(startAt)
}
foreach n in (9){
ve:=vanEck(n);
println("The first ten terms of the Van Eck (%d) sequence are:".fmt(n));
println("\t",ve.walk(10).concat(","));
println(" Terms 991 to 1000 of the sequence are:");
println("\t",ve.drop(990-10).walk(10).concat(","));
}
- Output:
The first ten terms of the Van Eck (0) 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 The first ten terms of the Van Eck (1) sequence are: 1,0,0,1,3,0,3,2,0,3 Terms 991 to 1000 of the sequence are: 0,6,53,114,302,0,5,9,22,71 The first ten terms of the Van Eck (2) sequence are: 2,0,0,1,0,2,5,0,3,0 Terms 991 to 1000 of the sequence are: 8,92,186,0,5,19,41,413,0,5 The first ten terms of the Van Eck (3) sequence are: 3,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 5,5,1,17,192,0,6,34,38,179 The first ten terms of the Van Eck (4) sequence are: 4,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 33,410,0,6,149,0,3,267,0,3 The first ten terms of the Van Eck (5) sequence are: 5,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 60,459,0,7,13,243,0,4,10,211 The first ten terms of the Van Eck (6) sequence are: 6,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 6,19,11,59,292,0,6,6,1,12 The first ten terms of the Van Eck (7) sequence are: 7,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 11,7,2,7,2,2,1,34,24,238 The first ten terms of the Van Eck (8) sequence are: 8,0,0,1,0,2,0,2,2,1 Terms 991 to 1000 of the sequence are: 16,183,0,6,21,10,249,0,5,48
- Programming Tasks
- Solutions by Programming Task
- 11l
- 8080 Assembly
- 8086 Assembly
- AArch64 Assembly
- ABC
- Action!
- Ada
- ALGOL 68
- ALGOL-M
- ALGOL W
- Amazing Hopper
- APL
- AppleScript
- ARM Assembly
- Arturo
- AWK
- BASIC
- BASIC256
- Chipmunk Basic
- Craft Basic
- FreeBASIC
- GW-BASIC
- PureBasic
- QBasic
- XBasic
- Yabasic
- BCPL
- BQN
- C
- C sharp
- C++
- Clojure
- CLU
- COBOL
- Comal
- Common Lisp
- Cowgol
- D
- Delphi
- Draco
- Dyalect
- EasyLang
- F Sharp
- Factor
- Fortran
- Fōrmulæ
- Go
- Haskell
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lua
- MACRO-11
- MAD
- Mathematica
- Wolfram Language
- Miranda
- Modula-2
- Nim
- Pascal
- Perl
- Phix
- Picat
- PL/M
- Prolog
- Python
- Quackery
- Racket
- Raku
- Refal
- REXX
- RPL
- Ruby
- Rust
- Scala
- SETL
- SNOBOL4
- Sidef
- Swift
- Tcl
- Uiua
- Visual Basic .NET
- V (Vlang)
- VTL-2
- Wren
- XPL0
- Zkl