RosettaCodeData/Task/Van-Eck-sequence/Oberon-07/van-eck-sequence.oberon
2026-04-30 12:34:36 -04:00

36 lines
1.5 KiB
Text

MODULE VanEck; (* 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 *)
IMPORT Out;
VAR vSeq, vPos : ARRAY 1001 OF INTEGER;
i : INTEGER;
(* Stores the first LEN( seq ) - 1 elements of the Van Eck sequence into seq *)
(* ( element 0 is not used ), pos must be the same length as seq and is used as a *)
(* temporary work array *)
PROCEDURE getVanEck( VAR seq, pos : ARRAY OF INTEGER );
VAR i, j, n, prev : INTEGER;
BEGIN
n := LEN( seq ) - 1;
FOR i := 0 TO n DO seq[ i ] := 0; pos[ i ] := 0 END;
FOR i := 2 TO n DO
j := i - 1;
prev := seq[ j ];
IF pos[ prev ] # 0 THEN (* not a new element *)
seq[ i ] := j - pos[ prev ]
END;
pos[ prev ] := j
END
END getVanEck;
BEGIN
(* construct the first 1000 terms of the sequence *)
getVanEck( vSeq, vPos );
(* show the first and last 10 elements *)
FOR i := 1 TO 10 DO Out.String( " " );Out.Int( vSeq[ i ], 0 ) END;Out.Ln;
FOR i := LEN( vSeq ) - 10 TO LEN( vSeq ) - 1 DO Out.String( " " );Out.Int( vSeq[ i ], 0 ) END;Out.Ln
END VanEck.