RosettaCodeData/Task/Van-der-Corput-sequence/Oberon-07/van-der-corput-sequence.oberon
2026-04-30 12:34:36 -04:00

53 lines
1.6 KiB
Text

MODULE VanDerCorput; (* show members of the van der Corput sequence *)
(* in various bases - translated from the C sample *)
IMPORT Out;
(* returns the gcd of a and b *)
PROCEDURE gcd( a, b : INTEGER ) : INTEGER;
VAR n, p, q : INTEGER;
BEGIN
p := a;
q := b;
WHILE p # 0 DO n := p; p := q MOD p; q := n END
RETURN q
END gcd ;
(* returns the numerator and denominator of the nth member of *)
(* the van der Corput sequence in the specified base *)
PROCEDURE vc( nth, base : INTEGER; VAR n, d : INTEGER );
VAR p, q, g : INTEGER;
BEGIN
p := 0; q := 1; n := nth;
WHILE n # 0 DO
p := p * base;
INC( p, n MOD base );
q := q * base;
n := n DIV base
END;
(* return the numerator and denominator reduced by their gcd *)
g := gcd( p, q );
n := p DIV g;
d := q DIV g
END vc;
PROCEDURE task;
VAR i, b, n, d : INTEGER;
BEGIN
FOR b := 2 TO 5 DO
Out.String( "base " );Out.Int( b, 0 );Out.String( ":" );
FOR i := 0 TO 9 DO
vc( i, b, n, d );
IF n = 0 THEN
Out.String( " 0" )
ELSE
Out.String( " " );Out.Int( n, 0 );
Out.String( "/" );Out.Int( d, 0 )
END
END;
Out.Ln
END
END task ;
BEGIN
task
END VanDerCorput.