Van der Corput sequence: Difference between revisions

m
 
(7 intermediate revisions by 5 users not shown)
Line 321:
Base 4: 0.25000 0.50000 0.75000 0.06250 0.31250 0.56250 0.81250 0.12500 0.37500 0.62500
Base 5: 0.20000 0.40000 0.60000 0.80000 0.04000 0.24000 0.44000 0.64000 0.84000 0.08000</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
<syntaxhighlight lang="algol68">
BEGIN # show members of the van der Corput sequence in various bases #
# translated from the C sample #
 
# sets num and denom to the numerator and denominator of the nth member #
# of the van der Corput sequence in the specified base #
PROC vc = ( INT nth, base, REF INT num, denom )VOID:
BEGIN
INT p := 0, q := 1, n := nth;
 
WHILE n /= 0 DO
p *:= base +:= n MOD base;
q *:= base;
n OVERAB base
OD;
 
num := p;
denom := q;
 
# reduce the numerrator and denominator by their gcd #
WHILE p /= 0 DO n := p; p := q MOD p; q := n OD;
num OVERAB q;
denom OVERAB q
END # vc # ;
 
# task #
FOR b FROM 2 TO 5 DO
print( ( "base ", whole( b, 0 ), ":" ) );
FOR i FROM 0 TO 9 DO
INT d, n;
vc( i, b, n, d );
IF n /= 0
THEN print( ( " ", whole( n, 0 ), "/", whole( d, 0 ) ) )
ELSE print( ( " 0" ) )
FI
OD;
print( ( newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
base 3: 0 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27
base 4: 0 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8
base 5: 0 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25
</pre>
 
=={{header|Arturo}}==
Line 1,073 ⟶ 1,123:
 
Base 5: [0, 0.2, 0.4, 0.6, 0.8, 0.04, 0.24, 0.44, 0.64, 0.84]</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function VanDerCorput(N,Base: integer): double;
{Calculate binary value for numbers right of decimal}
var Value,Exponent,Digit: integer;
begin
Value:= N; Result:= 0; Exponent:= -1;
{D1 * Base^-1 + D2 * Base^-2 + D3 * Base^-3}
while Value > 0 do
begin
{Get digit in specified base}
Digit:=Value mod Base;
{Digit * Base^-Exponent}
Result:=Result + Digit * Power(Base,Exponent);
{Divide by base to put next digit in place}
Value:= Value div Base;
{Next exponent}
Dec(Exponent);
end;
end;
 
 
procedure ShowVanDerCorput(Memo: TMemo);
{Show Vander Coput numbers for bases 2..8 and items 1..9 }
var Base,N: integer;
var V: double;
var S: string;
begin
S:='';
for Base:=2 to 8 do
begin
S:=S+Format('Base %D:',[Base]);
for N:=1 to 10 do
begin
V:=VanDerCorput(N,Base);
S:=S+Format(' %1.5f',[V]);
end;
S:=S+CRLF;
end;
Memo.Lines.Add(S);
end;
 
 
 
 
</syntaxhighlight>
{{out}}
<pre>
Base 2: 0.50000 0.25000 0.75000 0.12500 0.62500 0.37500 0.87500 0.06250 0.56250 0.31250
Base 3: 0.33333 0.66667 0.11111 0.44444 0.77778 0.22222 0.55556 0.88889 0.03704 0.37037
Base 4: 0.25000 0.50000 0.75000 0.06250 0.31250 0.56250 0.81250 0.12500 0.37500 0.62500
Base 5: 0.20000 0.40000 0.60000 0.80000 0.04000 0.24000 0.44000 0.64000 0.84000 0.08000
Base 6: 0.16667 0.33333 0.50000 0.66667 0.83333 0.02778 0.19444 0.36111 0.52778 0.69444
Base 7: 0.14286 0.28571 0.42857 0.57143 0.71429 0.85714 0.02041 0.16327 0.30612 0.44898
Base 8: 0.12500 0.25000 0.37500 0.50000 0.62500 0.75000 0.87500 0.01563 0.14063 0.26563
 
Elapsed Time: 1.344 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
procfunc vdc b n . v .
s = 1
v = 0
while n > 0
s *= b
Line 1,085 ⟶ 1,199:
n = n div b
.
return v
.
for b = 2 to 5
write "base " & b & ":"
for n range0 10
callwrite " " & vdc b n v
write " " & v
.
print ""
Line 1,607 ⟶ 1,721:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Van_der_Corput_sequence}}
 
'''Solution'''
 
[[File:Fōrmulæ - Van der Corput sequence 01.png]]
 
'''Case 1. Van der Corput sequences for numbers 0 .. 25, in bases 2 to 10'''
 
[[File:Fōrmulæ - Van der Corput sequence 02.png]]
 
[[File:Fōrmulæ - Van der Corput sequence 03.png]]
 
'''Case 2. Numerical values'''
 
[[File:Fōrmulæ - Van der Corput sequence 04.png]]
 
[[File:Fōrmulæ - Van der Corput sequence 05.png]]
 
'''Case 3. A plot of Van der Corput sequence for values 0 to 500, in base 10'''
 
[[File:Fōrmulæ - Van der Corput sequence 06.png]]
 
[[File:Fōrmulæ - Van der Corput sequence 07.png]]
 
=={{header|Go}}==
Line 1,948 ⟶ 2,084:
return m
end</syntaxhighlight>
 
Alternative version, prints the sequence elements as fractions - based on the Algol 68 sample.
<syntaxhighlight lang="lua">
function vdc( nth, base ) -- returns the numerator & denominator of the sequence element n in base
local p, q, n = 0, 1, nth
while n ~= 0 do
p = p * base
p = p + n % base;
q = q * base;
n = math.floor( n / base )
end
local num, denom = p, q;
-- reduce the numerator and denominator by their gcd
while p ~= 0 do
n = p
p = q % p
q = n
end
num = math.floor( num / q )
denom = math.floor( denom / q )
return num, denom
end
for b = 2,5 do
io.write( "base ", b, ": " )
for n = 0,9 do
local num, denom = vdc( n, b )
io.write( " ", num ) if num ~= 0 then io.write( "/", denom ) end
end
io.write( "\n" )
end
</syntaxhighlight>
 
{{out}}
<pre>
base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
base 3: 0 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27
base 4: 0 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8
base 5: 0 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25
</pre>
 
=={{header|Maple}}==
Line 3,444 ⟶ 3,619:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var v2 = Fn.new { |n|
var p = 0.5
var r = 0
2,120

edits