Van Eck sequence: Difference between revisions

Added Algol 68
m (Added Delphi reference to Pascal code)
(Added Algol 68)
Line 402:
<pre>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</pre>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find elements of the Van Eck Sequence - first term is 0, following #
# terms are 0 if the previous was the first appearance of the element #
# or how far back in the sequence the last element appeared #
# 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</lang>
{{out}}
<pre>
0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
</pre>
 
=={{header|ALGOL-M}}==
3,022

edits