Van der Corput sequence: Difference between revisions
m
→{{header|Fōrmulæ}}
m (→binary version: added/changed whitespace, used a template for the output section.) |
|||
(40 intermediate revisions by 25 users not shown) | |||
Line 29:
A ''hint'' at a way to generate members of the sequence is to modify a routine used to change the base of an integer:
<
digits = []
while n:
Line 37:
>>> base10change(11, 2)
[1, 0, 1, 1]</
the above showing that <code>11</code> in decimal is <math>1\times 2^3 + 0\times 2^2 + 1\times 2^1 + 1\times 2^0</math>.<br>
Reflected this would become <code>.1101</code> or <math>1\times 2^{-1} + 1\times 2^{-2} + 0\times 2^{-3} + 1\times 2^{-4}</math>
Line 55:
* [[wp:Van der Corput sequence|Van der Corput sequence]]
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F vdc(=n, base = 2)
V (vdc, denom) = (0.0, 1)
L n != 0
denom *= base
(n, V remainder) = divmod(n, base)
vdc += Float(remainder) / denom
R vdc
print((0.<10).map(i -> vdc(i)))
print((0.<10).map(i -> vdc(i, 3)))</syntaxhighlight>
{{out}}
<pre>
[0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625, 0.5625]
[0, 0.333333, 0.666667, 0.111111, 0.444444, 0.777778, 0.222222, 0.555556, 0.888889, 0.037037]
</pre>
=={{header|360 Assembly}}==
{{trans|BBC BASIC}}
The program uses two ASSIST macros (XDECO,XPRNT) to keep the code as short as possible.
<
VDCS CSECT
USING VDCS,R13 base register
Line 116 ⟶ 136:
XDEC DS CL12 work area for xdeco
YREGS
END VDCS</
{{out}}
<pre>
Line 130 ⟶ 150:
9 0.56250
10 0.31250
</pre>
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC Generate(INT value,base REAL POINTER res)
REAL denom,rbase,r1,r2
IntToReal(0,res)
IntToReal(1,denom)
IntToReal(base,rbase)
WHILE value#0
DO
RealMult(denom,rbase,r1)
RealAssign(r1,denom)
IntToReal(value MOD base,r1)
RealDiv(r1,denom,r2)
RealAdd(res,r2,r1)
RealAssign(r1,res)
value==/base
OD
RETURN
PROC Main()
INT value,base
REAL res
Put(125) PutE() ;clear the screen
FOR base=2 TO 5
DO
PrintF("Base %I:%E",base)
FOR value=0 TO 9
DO
Generate(value,base,res)
PrintR(res) Put(32)
OD
PutE() PutE()
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Van_der_Corput_sequence.png Screenshot from Atari 8-bit computer]
<pre>
Base 2:
0 .5 .25 .75 .125 .625 .375 .875 .0625 .5625
Base 3:
0 .3333333333 .6666666666 .1111111111 .4444444444 .7777777777 .2222222222 .5555555555 .8888888888 .037037037
Base 4:
0 .25 .5 .75 .0625 .3125 .5625 .8125 .125 .375
Base 5:
0 .2 .4 .6 .8 .04 .24 .44 .64 .84
</pre>
=={{header|ActionScript}}==
This implementation uses logarithms to computes the nth term of the sequence at any base. Numbers in the output are rounded to 6 decimal places to hide any floating point inaccuracies.
<syntaxhighlight lang="actionscript3">
package {
Line 201 ⟶ 275:
}
</syntaxhighlight>
{{out}}
<pre>
Line 214 ⟶ 288:
=={{header|Ada}}==
<
procedure Main is
Line 240 ⟶ 314:
Ada.Text_IO.New_Line;
end loop;
end Main;</
{{out}}
Line 247 ⟶ 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}}==
<syntaxhighlight lang="arturo">corput: function [num, base][
result: to :rational 0
b: 1 // base
n: num
while [not? zero? n][
result: result + b * n % base
n: n / base
b: b // base
]
return result
]
loop 2..5 'bs ->
print ["Base" bs ":" join.with:", " to [:string] map 1..10 'z -> corput z bs]</syntaxhighlight>
{{out}}
<pre>Base 2 : 1/2, 1/4, 3/4, 1/8, 5/8, 3/8, 7/8, 1/16, 9/16, 5/16
Base 3 : 1/3, 2/3, 1/9, 4/9, 7/9, 2/9, 5/9, 8/9, 1/27, 10/27
Base 4 : 1/4, 1/2, 3/4, 1/16, 5/16, 9/16, 13/16, 1/8, 3/8, 5/8
Base 5 : 1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25, 2/25</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
for i, v in [2, 3, 4, 5, 6] {
seq .= "Base " v ": "
Line 262 ⟶ 410:
r += Mod(n, b) * b ** -A_Index, n := n // b
return, r
}</
{{out}}
<pre>Base 2: 0, 0.50000, 0.25000, 0.75000, 0.12500, 0.62500, 0.37500, 0.87500, 0.06250, 0.56250
Line 271 ⟶ 419:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f VAN_DER_CORPUT_SEQUENCE.AWK
# converted from BBC BASIC
Line 299 ⟶ 447:
return(v)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 308 ⟶ 456:
5 0.00000 0.20000 0.40000 0.60000 0.80000 0.04000 0.24000 0.44000 0.64000 0.84000
</pre>
=={{header|BASIC}}==
{{trans|C}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR B=2 TO 5
30 PRINT USING "BASE #:";B;
40 FOR I=0 TO 9
50 P=0: Q=1: N=I
60 IF N=0 GOTO 110
70 P=P*B+N MOD B
80 Q=Q*B
90 N=N\B
100 GOTO 60
110 X=P: Y=Q
120 IF P=0 GOTO 150
130 N=P: P=Q MOD P: Q=N
140 GOTO 120
150 X=X\Q
160 Y=Y\Q
170 IF X=0 THEN PRINT " 0"; ELSE PRINT USING " ##/##";X;Y;
180 NEXT I
190 PRINT
200 NEXT B</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|BASIC256}}===
<syntaxhighlight lang="basic256">function num_base$(number, base)
if base > 9 then
print "base not handled by function"
pause 5
return ""
end if
ans$ = ""
while number <> 0
n = (number mod base)
ans$ = string(n) + ans$
number = number \ base
end while
if ans$ = "" then ans$ = "0"
return "." + ans$
end function
for k = 2 to 5
print "Base = "; k
for l = 0 to 12
print ljust(num_base$(l, k), 6);
next l
print : print
next k
end</syntaxhighlight>
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub num_base$(number, base)
if _base_ > 9 then
print "base not handled by function"
sleep 5000
return ""
end if
while number <> 0
n = mod(number, base)
ans$ = str$(n) + ans$
number = int(number / base)
wend
if ans$ = "" then ans$ = "0" : fi
return "." + ans$
end sub
for k = 2 to 5
print "Base = ", k
for l = 0 to 12
print left$(num_base$(l, k), 7), " ";
next l
print : print
next k
end</syntaxhighlight>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
FOR base% = 2 TO 5
PRINT "Base " ; STR$(base%) ":"
Line 329 ⟶ 564:
n% DIV= b%
ENDWHILE
= v</
{{out}}
<pre>
Line 345 ⟶ 580:
This solution hardcodes the literal <tt>10</tt> because [[Literals/Integer#bc|numeric literals in bc]] can use any base from 2 to 16. This solution only works with integer bases from 2 to 16.
<
* Return the _n_th term of the van der Corput sequence.
* Uses the current _ibase_.
Line 383 ⟶ 618:
obase = t
}
quit</
Some of the calculations are not exact, because bc performs calculations using base 10. So the program prints a result like <tt>.202222221</tt> (base 3) when the exact result would be <tt>.21</tt> (base 3).
Line 421 ⟶ 656:
.0200000
.1200000</pre>
=={{header|BCPL}}==
{{trans|C}}
<syntaxhighlight lang="bcpl">get "libhdr"
let corput(n, base, num, denom) be
$( let p = 0 and q = 1
until n=0
$( p := p * base + n rem base
q := q * base
n := n / base
$)
!num := p
!denom := q
until p=0
$( n := p
p := q rem p
q := n
$)
!num := !num / q
!denom := !denom / q
$)
let writefrac(num, denom) be
test num=0
do writes(" 0")
or writef(" %N/%N", num, denom)
let start() be
$( let num = ? and denom = ?
for base=2 to 5
$( writef("base %N:", base)
for i=0 to 9
$( corput(i, base, @num, @denom)
writefrac(num, denom)
$)
wrch('*N')
$)
$)</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|C}}==
<
void vc(int n, int base, int *num, int *denom)
Line 457 ⟶ 739:
return 0;
}</
{{out}}
<pre>base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
Line 472 ⟶ 754:
whose items are the numerator and denominator for the item.
<syntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 601 ⟶ 883:
}
</syntaxhighlight>
{{out}}
Line 613 ⟶ 895:
=={{header|C++}}==
{{trans|Raku}}
<
#include <iostream>
Line 638 ⟶ 920:
std::cout << "\n\n";
}
}</
{{out}}
<pre>
Line 655 ⟶ 937:
=={{header|Clojure}}==
<
"Get the nth element of the van der Corput sequence."
([n]
Line 679 ⟶ 961:
(into {:base base}
(for [n (range 10)] ;; table entries
[n (van-der-corput n base)]))))</
{{out}}
Line 688 ⟶ 970:
| 4 | 0 | 1/4 | 1/2 | 3/4 | 1/16 | 5/16 | 9/16 | 13/16 | 1/8 | 3/8 |
| 5 | 0 | 1/5 | 2/5 | 3/5 | 4/5 | 1/25 | 6/25 | 11/25 | 16/25 | 21/25 |</pre>
=={{header|CLU}}==
{{trans|C}}
<syntaxhighlight lang="clu">vc = proc (n, base: int) returns (int, int)
p: int := 0
q: int := 1
while n ~= 0 do
p := p * base + n // base
q := q * base
n := n / base
end
num: int := p
denom: int := q
while p ~= 0 do
p, q := q // p, p
end
return(num/q, denom/q)
end vc
print_frac = proc (po: stream, num, denom: int)
if num=0 then
stream$puts(po, " 0")
else
stream$puts(po, " ")
stream$putright(po, int$unparse(num), 2)
stream$puts(po, "/")
stream$putright(po, int$unparse(denom), 2)
end
end print_frac
start_up = proc ()
po: stream := stream$primary_output()
for base: int in int$from_to(2,5) do
stream$puts(po, "base " || int$unparse(base) || ":")
for i: int in int$from_to(0, 9) do
n, d: int := vc(i, base)
print_frac(po, n, d)
end
stream$putl(po, "")
end
end start_up</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|Common Lisp}}==
<
(loop for d = 1 then (* d base) while (<= d n)
finally
Line 700 ⟶ 1,028:
(loop for base from 2 to 5 do
(format t "Base ~a: ~{~6a~^~}~%" base
(loop for i to 10 collect (van-der-Corput i base))))</
{{out}}
<pre>Base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16 5/16
Line 706 ⟶ 1,034:
Base 4: 0 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8 5/8
Base 5: 0 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25 2/25</pre>
=={{header|Cowgol}}==
{{trans|C}}
<syntaxhighlight lang="cowgol">include "cowgol.coh";
sub vc(n: uint16, base: uint16): (num: uint16, denom: uint16) is
var p: uint16 := 0;
var q: uint16 := 1;
while n != 0 loop
p := p * base + n % base;
q := q * base;
n := n / base;
end loop;
num := p;
denom := q;
while p != 0 loop
n := p;
p := q % p;
q := n;
end loop;
num := num / q;
denom := denom / q;
end sub;
sub printfrac(num: uint16, denom: uint16) is
if num == 0 then
print(" 0");
else
print(" ");
print_i16(num);
print("/");
print_i16(denom);
end if;
end sub;
var i: uint16;
var base: uint16;
var num: uint16;
var denom: uint16;
base := 2;
while base < 6 loop
print("base ");
print_i16(base);
print(":");
i := 0;
while i < 10 loop
(num, denom) := vc(i, base);
printfrac(num, denom);
i := i + 1;
end loop;
print_nl();
base := base + 1;
end loop;</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|D}}==
<
double vdc = 0.0, denom = 1.0;
while (n) {
Line 723 ⟶ 1,114:
foreach (immutable b; 2 .. 6)
writeln("\nBase ", b, ": ", 10.iota.map!(n => vdc(n, b)));
}</
{{out}}
<pre>Base 2: [0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625, 0.5625]
Line 732 ⟶ 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">
func vdc
while n > 0
s *= b
m = n mod b
v += m / s
n = n div b
.
return v
.
for b = 2 to 5
write "base " & b & ":"
for n
.
print ""
.
</syntaxhighlight>
{{out}}
Line 758 ⟶ 1,215:
base 4: 0 0.25 0.50 0.75 0.06 0.31 0.56 0.81 0.12 0.38
base 5: 0 0.20 0.40 0.60 0.80 0.04 0.24 0.44 0.64 0.84</pre>
=={{header|EDSAC order code}}==
Base 2 only, extra credit not attempted.
EDSAC's fixed-point arithmetic makes this task in base 2 fairly straightforward. If, as usual, a 17-bit integer n is stored as n/(2^16), then to find the n'th term in the van der Corput sequence we just reverse the order of bits after the binary point. E.g. 13 is stored as 0.0000000000001101, so the 13th term is 0.1011000000000000 = 11/16. Similarly for 35-bit numbers. The demo program contains subroutines for both formats.
<syntaxhighlight lang="edsac">
[Van der Corput sequence for Rosetta Code.
EDSAC solution, Initial Orders 2.]
[Library subroutine M3 - prints header at load time and is then overwritten.
Here, the last character sets the teleprinter to figures.]
PFGKIFAFRDLFUFOFE@A6FG@E8FEZPF
*VAN!DER!CORPUT!SEQUENCE@A*BIT!!!#35A*BIT@&#
..PZ [blank tape then re-sync]
[Define load addresses]
T55K P100F [V parameter: van der Corput subroutines]
T51K P64F [G parameter: print subroutine]
T47K P400F [M parameter: main routine]
[Subroutines to return n'th element of van der Corput sequence.
17-bit version: Call by GV, pass n in 0F (not preserved), result in 4F.
35-bit version: Call by G1V, pass n in 0D (not preserved), result in 4D.]
E25K TV GK
G2@ [jump to 17-bit version]
G25@ [jump to 35-bit version]
[17-bit version.
On EDSAC, it's a matter of reversing the bits after the binary point
To save time, we use a table to reverse the 16 bits in groups of 4.]
[2] A3F T24@ [plant return link as usual]
H5 6@ [set mult reg to 0...01111 binary]
A55@ T4F [set marker bit 0...01 in result]
[7] A4F L4F T4F [shift result 4 left]
CF [acc := next 4 bits of n]
LD [shift into address field]
A58@ T14@ [plant A order to load from table]
[14] AF [{planted) load bits from table]
A4F [add to result]
G22@ [jump out if marker bit has reached sign bit]
T4F [update result]
AF R4F TF [shift n 4 right]
E7@ [always loop back]
[22] S57@ [done, remove marker bit]
T4F [store final result]
[24] ZF [(planted) jump to return to caller]
[35-bit version. Very similar to the 17-bit version, except that
after reversing 8 groups of 4, there are 2 bits left over,
which require separate treatment.]
[25] A3F T54@ [plant return link as usual]
H56@ [set mult reg to 0...01111 binary]
YF L2F [set marker bit 0...0100 in result]
[30] L4F T4D [shift result 4 left]
CF LD A58@ T36@ AF A4F T4F [update from table as in 17-bit version]
ADR4FTD [shift n 4 right]
A4D [load result]
E30@ [if marker bit hasn't reached sign bit, loop back]
[Last 2 bits]
[44] L1FT4D [shift result 2 right]
CF LD A58@ T50@ [plant A order as in 17-bit version]
[50] AF [Planted) load bits from table]
R1F A4F T4F [shift table entry 2 right and add to result]
[54] ZF [(planted) jump to return to caller]
[Constants]
[55] PD [17-bit 1]
[56] P7D [17-bit 15]
[57] K4096F [17-bit 10...0 binary]
[58] A59@ [order to load from table{0}]
[Table to reverse group of 4 bits, e.g. table{0010b} = 0100b]
[59] PFP4FP2FP6FP1FP5FP3FP7FPDP4DP2DP6DP1DP5DP3DP7D
[Library subroutine P1 to print number in range 0 <= x < 1.
Caller must print leading '0.' if required. 21 storage locations.]
E25K TG
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F
[Main routine]
E25K TM GK
[0] PF PF [n, 35 bits, must be at even address]
[2] PF [negative count of terms]
[3] P10F [<=== EDIT number of terms, in address field]
[4] PD [17-bit integer 1]
[5] MF [dot (in figures mode)]
[6] @F [carriage return]
[7] &F [line feed]
[8] !F [space character]
[9] K4096F [null character]
[Enter with acc = 0]
[10] T#@ [n := 0]
S3@ T2@ [initialize negative count]
[13] A@ TF [pass 17-bit n in 0F]
[15] A15@ GV [call 17-bit van der Corput routine]
TD [clear 0D, including sandwich bit]
A4F T1F [extend 17-bit result to 35 bits in 0D]
O4@ O5@ [print '0.']
[22] A22@ GG P5F [print result to 5 decimals]
O8@ O8@ [print 2 spaces]
A#@ TD [pass 35-bit n in 0D]
[29] A29@ G1V [call 35-bit van der Corput routine]
A4D TD [pass result in 0D]
O4@ O5@ [print '0.']
[35] A35@ GG P10F [print result to 10 decimals]
O6@ O7@ [print CR LF]
A2@ A2F [inc negative count]
E48@ [jump out if count = 0]
T2@ [update count]
A@ A4@ T@ [inc n]
E13@ [loop back]
[48] O9@ [print null to flush teleprinter buffer]
ZF [stop]
E10Z [define entry point]
PF [acc = 0 on entry]
[end]
</syntaxhighlight>
{{out}}
<pre>
VAN DER CORPUT SEQUENCE
17-BIT 35-BIT
0.00000 0.0000000000
0.50000 0.5000000000
0.25000 0.2500000000
0.75000 0.7500000000
0.12500 0.1250000000
0.62500 0.6250000000
0.37500 0.3750000000
0.87500 0.8750000000
0.06250 0.0625000000
0.56250 0.5625000000
</pre>
=={{header|Ela}}==
<
vdc bs n = vdc' 0.0 1.0 n
Line 770 ⟶ 1,358:
rem = n % bs
n' = truncate (n / bs)
v' = v + rem / d'</
Test (with base 2.0, using non-strict map function on infinite list):
<
{{out}}
Line 781 ⟶ 1,369:
=={{header|Elixir}}==
{{works with|Elixir|1.1}}
<
def sequence( n, base \\ 2 ) do
"0." <> (Integer.to_string(n, base) |> String.reverse )
Line 814 ⟶ 1,402:
IO.puts " Base #{ base }: #{ Enum.map_join(0..9, ", ", &fun.(&1, base)) }"
end)
end)</
{{out}}
Line 837 ⟶ 1,425:
=={{header|Erlang}}==
I liked the bc output-in-same-base, but think this is the way it should look.
<syntaxhighlight lang="erlang">
-module( van_der_corput ).
Line 861 ⟶ 1,449:
[io:fwrite( " ~p", [sequence(X, Base)] ) || X <- lists:seq(0, 9)],
io:fwrite( "~n" ).
</syntaxhighlight>
{{out}}
<pre>
Line 872 ⟶ 1,460:
=={{header|ERRE}}==
<
!
Line 898 ⟶ 1,486:
PRINT
END FOR
END PROGRAM</
{{out}}
<pre>
Line 913 ⟶ 1,501:
=={{header|Euphoria}}==
{{trans|D}}
<
atom vdc, denom, rem
vdc = 0
Line 932 ⟶ 1,520:
end for
puts(1,"\n\n")
end for</
{{out}}
Line 950 ⟶ 1,538:
=={{header|F_Sharp|F#}}==
<
let vdc n b =
Line 965 ⟶ 1,553:
printfn "%A" [ for n in 0 .. 9 -> (vdc n 2) ]
printfn "%A" [ for n in 0 .. 9 -> (vdc n 5) ]
0</
{{out}}
<pre>[0.0; 0.5; 0.25; 0.75; 0.125; 0.625; 0.375; 0.875; 0.0625; 0.5625]
Line 972 ⟶ 1,560:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<
math.ranges sequences ;
IN: rosetta-code.van-der-corput
Line 986 ⟶ 1,574:
] each ;
MAIN: vdc-demo</
{{out}}
<pre>
Line 996 ⟶ 1,584:
=={{header|Forth}}==
<
0e 1e ( F: vdc denominator )
begin dup while
Line 1,005 ⟶ 1,593:
repeat 2drop fdrop ;
: test 10 0 do 2 i fvdc cr f. loop ;</
{{out}}
Line 1,021 ⟶ 1,609:
=={{header|Fortran}}==
This is straightforward once one remembers that the obvious scheme for extracting digits from a number produces them from the low-order end to the high-order end. This reversal is normally annoying, but here a "reflection" ''is'' desired. The source is old-style, except for using F90's ability to have a function (or subroutine) name appear on its END statement with this checked by the compiler. Because the MODULE protocol introduced by F90 is not bothered with, the type of the function has to be declared in all routines invoking it if the default type based on the form of the name does not suffice. Single precision suffices, but the F90 compiler moans that the type of the function itself has not been explicitly declared. Ah well. <
Converts 1234 in decimal to 4321 in V, and P = 10000.
INTEGER N !For this integer,
Line 1,055 ⟶ 1,643:
END DO !On to the next base.
END</
Output: six-digit precision is about the most that single precision offers.
<pre>
Line 1,075 ⟶ 1,663:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,116 ⟶ 1,704:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Base = 2
Line 1,129 ⟶ 1,717:
Base = 5
.0 .1 .2 .3 .4 .10 .11 .12 .13 .14 .20 .21 .22</pre>
=={{header|Fōrmulæ}}==
{{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}}==
<
import "fmt"
Line 1,170 ⟶ 1,784:
fmt.Println(i, v3(i))
}
}</
{{out}}
<pre>
Line 1,199 ⟶ 1,813:
=={{header|Haskell}}==
The function <tt>vdc</tt> returns the n<sup>th</sup> exact, arbitrary precision van der Corput number for any base ≥ 2 and any n. (A reasonable value is returned for negative values of n.)
<
import Data.
import
-- A wrapper type for Rationals to make them look nicer when we print them.
newtype Rat =
Rat Rational
instance Show Rat where
show (Rat n) = show (numerator n)
-- Convert a list of base b digits to its corresponding number.
-- We assume the digits are valid base b numbers and that
-- their order is from least to most significant.
digitsToNum :: Integer -> [Integer] -> Integer
digitsToNum b = foldr1 (\d acc -> b * acc + d)
-- Convert a number to the list of its base b digits.
-- The order will be from least to most significant.
numToDigits :: Integer -> Integer -> [Integer]
numToDigits _ 0 = [0]
numToDigits b n = unfoldr step n
where
step m =
let (q, r) = m `quotRem` b
in Just (r, q)
-- Return the n'th element in the base b van der Corput sequence.
-- The base must be ≥ 2.
vdc :: Integer -> Integer -> Rat
vdc b n
| b <
| otherwise =
let ds = reverse $ numToDigits b n
in Rat (digitsToNum b ds % b ^ length ds)
-- Each base followed by a specified range of van der Corput numbers.
printVdcRanges :: ([Integer], [Integer]) -> IO ()
printVdcRanges (bases, nums) =
mapM_
putStrLn
[ printf "Base %d:" b <> concatMap (printf " %5s" . show) rs
| b <- bases
, let rs = map (vdc b) nums ]
main :: IO ()
main = do
-- Small bases:
printVdcRanges ([2, 3, 4, 5], [0 .. 9])
putStrLn []
-- Base 123:
printVdcRanges ([123], [50,100 .. 300])</syntaxhighlight>
{{out}}
<pre>Base 2: 0/1 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
Base 3: 0/1 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27
Base 4: 0/1 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8
Base 5: 0/1 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25
Base 123: 50/123 100/123 3322/15129 9472/15129 494/15129 6644/15129</pre>
=={{header|Icon}} and {{header|Unicon}}==
The following solution works in both Icon and Unicon:
<
base := integer(get(A)) | 2
every writes(round(vdc(0 to 9,base),10)," ")
Line 1,277 ⟶ 1,893:
places := 10 ^ d
return real(integer(n*places + 0.5)) / places
end</
and a sample run is:
Line 1,293 ⟶ 1,909:
An alternate, Unicon-specific implementation of <tt>vdc</tt> patterned after the functional Raku
solution is:
<
s1 := create |((0 < 1(.n, n /:= base)) % base)
s2 := create 2(e := 1.0, |(e *:= base))
every (result := 0) +:= |s1() / s2()
return result
end</
It produces the same output as shown above.
=={{header|J}}==
'''Solution:'''
<
'''Examples:'''
<
0 0.5 0.25 0.75 0.125 0.625 0.375 0.875 0.0625 0.5625
2x vdc i.10 NB. as above but using rational nums
Line 1,313 ⟶ 1,929:
0 1r3 2r3 1r9 4r9 7r9 2r9 5r9 8r9 1r27
0 1r4 1r2 3r4 1r16 5r16 9r16 13r16 1r8 3r8
0 1r5 2r5 3r5 4r5 1r25 6r25 11r25 16r25 21r25</
In other words: use the left argument as the "base" to structure the sequence numbers into digits ("base 2", etc.). Then use the reciprocal of the left argument as the "base" to re-represent this sequence and divide that result by the left argument to get the Van der Corput sequence number.
Line 1,323 ⟶ 1,939:
Comparing it to the behavior of parentheses, it looks like it should do the doubling and assignment first. Luckily for us, it works the same in Java as in Raku (doubling and assignment first). It was kept the Raku way to help with the comparison.
Normally, we would initialize denom to 2 (since that is the denominator of the leftmost digit), use it alone in the vdc sum, and then double it after.
<
public static double vdc(int n){
double vdc = 0;
Line 1,339 ⟶ 1,955:
}
}
}</
{{out}}
<pre>0.0
Line 1,357 ⟶ 1,973:
The neat thing about the following implementation of vdc(base) is that it shows how the task can be accomplished in two separate steps without the need to construct an intermediate array.
<
# Corput sequence using base 'base', e.g. (4 | vdc(2)) is 0.125.
#
Line 1,372 ⟶ 1,988:
if . == 0 then 0
else decimalize(recurse( if . == 0 then empty else ./base | floor end ) % base)
end ;</
'''Example:'''
<
(if . < 0 then -1 else 1 end) as $s
| $s*10*.*n | if (floor%10)>4 then (.+5) else . end | ./10 | floor/n | .*$s;
range(2;6) | . as $base | "Base \(.): \( [ range(0;11) | vdc($base)|round(1000) ] )"</
{{out}}
<syntaxhighlight lang="sh">
$ jq -n -f -c -r van_der_corput_sequence.jq
Base 2: [0,0.5,0.25,0.75,0.125,0.625,0.375,0.875,0.063,0.563,0.313]
Base 3: [0,0.333,0.667,0.111,0.444,0.778,0.222,0.556,0.889,0.037,0.37]
Base 4: [0,0.25,0.5,0.75,0.063,0.313,0.563,0.813,0.125,0.375,0.625]
Base 5: [0,0.2,0.4,0.6,0.8,0.04,0.24,0.44,0.64,0.84,0.08]</
=={{header|Julia}}==
<
vandercorput(num::Integer, base::Integer) = sum(d * Float64(base) ^ -ex for (ex, d) in enumerate(digits(num, base = base)))
for base in 2:9
Line 1,396 ⟶ 2,012:
for num in 0:9 @printf("%7.3f", vandercorput(num, base)) end
println(" [...]")
end</
{{out}}
Line 1,411 ⟶ 2,027:
=={{header|Kotlin}}==
{{trans|C}}
<
data class Rational(val num: Int, val denom: Int)
Line 1,444 ⟶ 2,060:
println()
}
}</
{{out}}
Line 1,455 ⟶ 2,071:
=={{header|Lua}}==
<
local digits = {}
while n ~= 0 do
Line 1,467 ⟶ 2,083:
end
return m
end</
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}}==
<syntaxhighlight lang="maple">Halton:=proc(n,b)
local i:=n,k:=1,s:=0,r;
while i>0 do
k/=b;
i:=iquo(i,b,'r');
s+=k*r
od;
s
end;
map(Halton,[$1..10],2);
map(Halton,[$1..10],3);
# [1/3, 2/3, 1/9, 4/9, 7/9, 2/9, 5/9, 8/9, 1/27, 10/27]
map(Halton,[$1..10],4);
# [1/4, 1/2, 3/4, 1/16, 5/16, 9/16, 13/16, 1/8, 3/8, 5/8]
map(Halton,[$1..10],5);
[1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25, 2/25]</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">VanDerCorput[n_,base_:2]:=Table[
FromDigits[{Reverse[IntegerDigits[k,base]],0},base],
{k,n}]
VanDerCorput[10,2]
VanDerCorput[10,3]
VanDerCorput[10,4]
VanDerCorput[10,5]
</syntaxhighlight>
{{out}}
<pre>{1/2,1/4,3/4,1/8,5/8,3/8,7/8,1/16,9/16,5/16}
{1/3, 2/3, 1/9, 4/9, 7/9, 2/9, 5/9, 8/9, 1/27, 10/27}
{1/4, 1/2, 3/4, 1/16, 5/16, 9/16, 13/16, 1/8, 3/8, 5/8}
{1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25, 2/25}</pre>
=={{header|MATLAB}} / {{header|Octave}}==
<
b = dec2bin(1:n)-'0'; % generate sequence of binary numbers from 1 to n
l = size(b,2); % get number of binary digits
w = (1:l)-l-1; % 2.^w are the weights
x = b * ( 2.^w'); % matrix times vector multiplication for
end; </
{{out}}
Line 1,514 ⟶ 2,190:
Define two helper functions
<
dec2digits(d, base):= block([digits: []],
while (d>0) do block([newdi: mod(d, base)],
Line 1,524 ⟶ 2,200:
/* [1, 2, 3] */
dec2digits( 8, 2);
/* [1, 0, 0, 0] */</
<
digits2dec(l, base):= block([s: 0, po: 1],
for di in reverse(l) do (s: di*po + s, po: po*base),
Line 1,534 ⟶ 2,210:
/* 123 */
digits2dec([1, 0, 0, 0], 2);
/* 8 */</
The main function
<
digits2dec(
dec2digits(k, base),
Line 1,555 ⟶ 2,231:
(%o124) [-, -, -, -, --, --, --, --, --, --]
5 5 5 5 25 25 25 25 25 25
*/</
<tt>digits2dec</tt> can by used with symbols to produce the same example as in
the task description
<syntaxhighlight lang="maxima">
/* 11 in decimal is */
digits: digits2dec([box(1), box(0), box(1), box(1)], box(2));
Line 1,580 ⟶ 2,256:
""" """ """ """ """ """ """ """
*/</
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,632 ⟶ 2,308:
ReadChar
END Sequence.</
=={{header|Nim}}==
Using the “rationals” module of the standard library.
<syntaxhighlight lang="nim">import rationals, strutils, sugar
type Fract = Rational[int]
proc corput(n: int; base: Positive): Fract =
result = 0.toRational
var b = 1 // base
var n = n
while n != 0:
result += n mod base * b
n = n div base
b /= base
for base in 2..5:
let list = collect(newSeq, for n in 1..10: corput(n, base))
echo "Base $#: ".format(base), list.join(" ")</syntaxhighlight>
{{out}}
<pre>Base 2: 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16 5/16
Base 3: 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27 10/27
Base 4: 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8 5/8
Base 5: 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25 2/25</pre>
=={{header|PARI/GP}}==
<
VdC(n)=sum(i=1,#binary(n),if(bittest(n,i-1),1.>>i)); \\ Alternate approach
vector(10,n,VdC(n))</
{{out}}
<pre>[0.500000000, 0.250000000, 0.750000000, 0.125000000, 0.625000000, 0.375000000, 0.875000000, 0.0625000000, 0.562500000, 0.312500000]</pre>
Line 1,643 ⟶ 2,344:
=={{header|Pascal}}==
Tested with Free Pascal
<
{$IFDEF FPC}
{$MODE DELPHI}
Line 1,731 ⟶ 2,432:
end;
end.
</syntaxhighlight>
;output:
<pre> Base 2 :0/1 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
Line 1,740 ⟶ 2,441:
=={{header|Perl}}==
{{trans|Raku}}
<
my @value = shift;
my $base = shift // 2;
Line 1,754 ⟶ 2,455:
print "base $base: ", join ' ', map { vdc($_, $base) } 0 .. 10;
print "\n";
}</
=={{header|Phix}}==
Not entirely sure what to print, so decided to print in three different ways.<br>
It struck me straightaway that the VdC of say 123 is 321/1000, which seems trivial in any base or desired format.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">enum</span> <span style="color: #000000;">BASE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">FRAC</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DECIMAL</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">DESC</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Base"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Fraction"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Decimal"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">vdc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">num</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">denom</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">denom</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">base</span>
<span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">=</span><span style="color: #000000;">BASE</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
<span
<span style="color: #000000;">num</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">*</span><span style="color: #000000;">base</span><span style="color: #0000FF;">+</span><span style="color: #000000;">digit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">=</span><span style="color: #000000;">FRAC</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">/</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">/</span><span style="color: #000000;">g</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">=</span><span style="color: #000000;">DECIMAL</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">/</span><span style="color: #000000;">denom</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"0."</span><span style="color: #0000FF;">&</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_vdc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">v</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</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;">"%s %d: "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">DESC</span><span style="color: #0000FF;">[</span><span style="color: #000000;">flag</span><span style="color: #0000FF;">],</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">vdc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">flag</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">=</span><span style="color: #000000;">FRAC</span> <span style="color: #008080;">and</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</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;">"0 "</span><span style="color: #0000FF;">)</span>
<span style="color:
<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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">show_vdc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">BASE</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s "</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">show_vdc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">FRAC</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d/%d "</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">show_vdc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECIMAL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%g "</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,820 ⟶ 2,523:
=={{header|PicoLisp}}==
<
(de vdc (N B)
Line 1,833 ⟶ 2,536:
(prinl "Base: " B)
(for N (range 0 9)
(prinl N ": " (round (vdc N B) 4)) ) )</
{{out}}
<pre style="height:20em;overflow:scroll">Base: 2
Line 1,870 ⟶ 2,573:
=={{header|PL/I}}==
<syntaxhighlight lang="text">
vdcb: procedure (an) returns (bit (31)); /* 6 July 2012 */
declare an fixed binary (31);
Line 1,889 ⟶ 2,592:
put skip list ('0.' || vdcb(i));
end;
</syntaxhighlight>
{{out}}
<pre>
Line 1,906 ⟶ 2,609:
=={{header|Prolog}}==
=== Example solution ===
<syntaxhighlight lang="prolog">% vdc( N, Base, Out )
% Out = the Van der Corput representation of N in given Base
vdc( 0, _, [] ).
Line 1,947 ⟶ 2,651:
write('7th member in base 4 (stretch goal) => '),
print_vdc( 7, 4 ).
</syntaxhighlight>
{{out}} (result of test):
Line 1,964 ⟶ 2,668:
7th member in base 4 (stretch goal) => 0.31
true .
</pre>
=== Solution with generator ===
<syntaxhighlight lang="prolog">
% g(B,N,X):- consecutively generate in X the first N elements of the sequence based on {0, 1, ..., B}
g(_,N,[L|_]-_,X):- N > 1, atomic_list_concat(['0.'|L],X).
g(B,N,[L|Ls]-Xs,X):- N > 2, M is N-1, findall([I|L], between(0,B,I), T), append(T,Ys,Xs), g(B,M,Ls-Ys,X).
g(_,N,'0.0'):- N > 0.
g(B,N,X):- N > 0, findall([I], between(1,B,I), T), T \= [], append(T,Ys,Xs), g(B,N,Xs-Ys,X).
</syntaxhighlight>
{{out}}
<pre>
?- g(2,10,X).
X = '0.0' ;
X = '0.1' ;
X = '0.2' ;
X = '0.01' ;
...
X = '0.001' ;
false.
?- time(findall(X, g(1,1000000,X), T)).
% 23,000,011 inferences, 5.938 CPU in 6.083 seconds (98% CPU, 3873686 Lips)
T = ['0.0', '0.1', '0.01', '0.11', '0.001', '0.101', '0.011', '0.111', '0.0001'|...].
</pre>
=={{header|PureBasic}}==
<
Define r.d,s.i=1
While n
Line 1,986 ⟶ 2,715:
PrintN("")
Next
Input()</
{{out}}
<pre>Base 2: 0.00000 0.50000 0.25000 0.75000 0.12500 0.62500 0.37500 0.87500 0.06250 0.56250
Line 1,997 ⟶ 2,726:
The multi-base sequence generator
<
vdc, denom = 0,1
while n:
Line 2,003 ⟶ 2,732:
n, remainder = divmod(n, base)
vdc += remainder / denom
return vdc</
'''Sample output'''
Base 2 and then 3:
<
[0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625, 0.5625]
>>> [vdc(i, 3) for i in range(10)]
[0, 0.3333333333333333, 0.6666666666666666, 0.1111111111111111, 0.4444444444444444, 0.7777777777777777, 0.2222222222222222, 0.5555555555555556, 0.8888888888888888, 0.037037037037037035]
>>> </
===As fractions===
We can get the output as rational numbers if we use the fraction module
(and change its string representation to look like a fraction):
<
>>> Fraction.__repr__ = lambda x: '%i/%i' % (x.numerator, x.denominator)
>>> [vdc(i, base=Fraction(2)) for i in range(10)]
[0, 1/2, 1/4, 3/4, 1/8, 5/8, 3/8, 7/8, 1/16, 9/16]</
===Stretch goal===
Sequences for different bases:
<
print('\nBase', b)
print([vdc(i, base=Fraction(b)) for i in range(10)])
Line 2,035 ⟶ 2,764:
Base 5
[0, 1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25]</
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ $ "bigrat.qky" loadfile ] now!
[ [] swap
[ dup while
base share /mod
rot join swap
again ]
drop ] is digits ( n --> [ )
[ base put
digits reverse
dup 0 swap
witheach
[ base share rot * + ]
base take rot size **
reduce ] is corput ( n n --> n/d )
5 times
[ say "base "
i^ 2 + dup echo
say ": "
10 times
[ i^ over corput
vulgar$ echo$ sp sp ]
cr drop ]</syntaxhighlight>
{{out}}
<pre>base 2: 0/1 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
base 3: 0/1 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27
base 4: 0/1 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8
base 5: 0/1 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25
base 6: 0/1 1/6 1/3 1/2 2/3 5/6 1/36 7/36 13/36 19/36
</pre>
=={{header|Racket}}==
Following the suggestion.
<
(define (van-der-Corput n base)
(if (zero? n)
Line 2,045 ⟶ 2,812:
(let-values ([(q r) (quotient/remainder n base)])
(/ (+ r (van-der-Corput q base))
base))))</
By digits, extracted arithmetically.
<
(define (digit-length n base)
(if (< n base) 1 (add1 (digit-length (quotient n base) base))))
Line 2,053 ⟶ 2,820:
(remainder (quotient n (expt base i)) base))
(define (van-der-Corput n base)
(for/sum ([i (digit-length n base)]) (/ (digit n i base) (expt base (+ i 1)))))</
Output.
<
(printf "Base ~a: " base)
(for ([n (in-range 0 10)])
Line 2,064 ⟶ 2,831:
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 |# </
=={{header|Raku}}==
Line 2,071 ⟶ 2,838:
First a cheap implementation in base 2, using string operations.
<syntaxhighlight lang="raku"
.say for VdC[^16];</
Here is a more elaborate version using the polymod built-in integer method:
<syntaxhighlight lang="raku"
map {
[+] $_ && .polymod($base xx *) Z/ [\*] $base xx *
Line 2,081 ⟶ 2,848:
}
.say for VdC[^10];</
{{out}}
<pre>0
Line 2,095 ⟶ 2,862:
Here is a fairly standard imperative version in which we mutate three variables in parallel:
<syntaxhighlight lang="raku"
my $n = $num;
my $vdc = 0;
Line 2,110 ⟶ 2,877:
say ( vdc($_,$b).Rat.nude.join('/') for ^10 ).join(', ');
say '';
}</
{{out}}
<pre>Base 2
Line 2,124 ⟶ 2,891:
0, 1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25</pre>
Here is a functional version that produces the same output:
<syntaxhighlight lang="raku"
my @values = $value, { $_ div $base } ... 0;
my @denoms = $base, { $_ * $base } ... *;
Line 2,130 ⟶ 2,897:
$v mod $base / $d;
}
}</
We first define two sequences, one finite, one infinite.
When we zip those sequences together, the finite sequence terminates the loop (which, since a Raku loop returns all its values, is merely another way of writing a <tt>map</tt>).
Line 2,144 ⟶ 2,911:
A range of integers (for output) is also supported.
<
numeric digits 1000 /*handle almost anything the user wants*/
parse arg a b . /*obtain the optional arguments from CL*/
Line 2,159 ⟶ 2,926:
VdC: procedure; y= x2b( d2x( arg(1) ) ) + 0 /*convert to hexadecimal, then binary.*/
if y==0 then return 0 /*handle the special case of zero. */
return '.'reverse(y) /*heavy lifting is performed by REXX. */</
{{out|output|text= when using the default input of: <tt> 0 10 </tt>
<pre>
Line 2,176 ⟶ 2,943:
===any radix up to 90===
This version handles what the first version does, plus any radix up to (and including) base '''90'''.
<br>It can also support a list (enabled when the base is negative).
<
/*
numeric digits 1000 /*handle almost anything the user wants*/
parse arg a b r . /*obtain optional arguments from the CL*/
if a=='' | a=="," then parse value 0 10 with a b /*Not specified? Then use the defaults*/
if b=='' | b=="," then b=
if r=='' | r=="," then r=
z= /*a placeholder for a list of numbers. */
do j=a to b /*traipse through the range of integers*/
_= VdC( abs(j), abs(r) )
_= substr('-', 2 + sign(j) )_
if r>0 then say _ /*if positive base, then just show it. */
else z=z _ /* ··· else append (build) a list. */
Line 2,205 ⟶ 2,972:
╚══════════════════════════════════════════════════════════════════════════════════╝*/
@abc= 'abcdefghijklmnopqrstuvwxyz' /*the lowercase Latin alphabet letters.*/
@abcU= @abc;
@@@= 0123456789 || @abc || @abcU /*prefix them with the decimal digits. */
@@@= @@@'<>[]{}()?~!@#$%^&*_+-=|\/;:`' /*add some special characters as well, */
/*──those chars should all be viewable.*/
numeric digits 1000 /*what the hey, support bigun' numbers.*/
maxB= length(@@@)
if toB=='' then toB=
if inB=='' then inB=
#=0 /* [↓] convert base inB X ──► base 10*/
do j=1 for length(x) /*process each "numeral" in the string.*/
_= substr(x, j, 1)
v= pos(_, @@@)
if v==0 | v>inB then call erd /*is it an illegal "digit" (numeral) ? */
#= # * inB + v - 1
end /*j*/
y= /* [↓] convert base 10 # ──► base toB.*/
do while #>=toB /*deconstruct the new number (#). */
y= substr(@@@, # // toB + 1, 1)y
#= # % toB
end /*while*/
Line 2,229 ⟶ 2,996:
/*──────────────────────────────────────────────────────────────────────────────────────*/
erd: say 'the character ' v " isn't a legal numeral for base " inB'.'; exit 13
VdC: return '.'reverse( base( arg(1), arg(2) )) /*convert the #, reverse the #, append.*/</syntaxhighlight>
(A ''negative'' base indicates to show numbers as a list.)
Line 2,255 ⟶ 3,021:
=={{header|Ring}}==
<
decimals(4)
for base = 2 to 5
Line 2,275 ⟶ 3,041:
end
return vdc
</syntaxhighlight>
Output:
<pre>
Line 2,283 ⟶ 3,049:
base 5 : 0 0.2000 0.4000 0.6000 0.8000 0.0400 0.2400 0.4400 0.6400 0.8400
</pre>
=={{header|RPL}}==
{{trans|Python}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → base
≪ 0 1 ROT
'''WHILE''' DUP '''REPEAT'''
SWAP base * SWAP
base / LAST MOD IP
3 PICK / 4 ROLL + ROT ROT
'''END''' DROP2
≫ ≫ 'VDC' STO
|
'''VDC''' ''( n base -- vdc )''
vdc, denom = 0,1
while n:
denom *= base
n, remainder = divmod(n, base)
vdc += remainder / denom
return vdc
|}
{{in}}
<pre>
≪ {} 0 9 FOR j j 2 VDC + NEXT ≫ EVAL
≪ {} 0 9 FOR j j 3 VDC + NEXT ≫ EVAL
</pre>
{{out}}
<pre>
2: { 0 0.5 0.25 0.75 0.125 0.625 0.375 0.875 0.0625 0.5625 }
1: { 0 0.333333333333 0.666666666667 0.111111111111 0.444444444444 0.777777777778 0.222222222222 0.555555555556 0.888888888889 3.7037037037E-02 }
</pre>
=={{header|Ruby}}==
The multi-base sequence generator
<
str = n.to_s(base).reverse
str.to_i(base).quo(base ** str.length)
Line 2,293 ⟶ 3,097:
(2..5).each do |base|
puts "Base #{base}: " + Array.new(10){|i| vdc(i,base)}.join(", ")
end</
'''Sample output'''
Line 2,301 ⟶ 3,105:
Base 4: 0/1, 1/4, 1/2, 3/4, 1/16, 5/16, 9/16, 13/16, 1/8, 3/8
Base 5: 0/1, 1/5, 2/5, 3/5, 4/5, 1/25, 6/25, 11/25, 16/25, 21/25
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
/// Van der Corput sequence for any base, based on C languange example from Wikipedia.
pub fn corput(nth: usize, base: usize) -> f64 {
let mut n = nth;
let mut q: f64 = 0.0;
let mut bk: f64 = 1.0 / (base as f64);
while n > 0_usize {
q += ((n % base) as f64)*bk;
n /= base;
bk /= base as f64;
}
q
}
fn main() {
for base in 2_usize..=5_usize {
print!("Base {}:", base);
for i in 1_usize..=10_usize {
let c = corput(i, base);
print!(" {:.6}", c)
}
println!("");
}
}
</syntaxhighlight>
{{out}}
<pre>
Base 2: 0.500000 0.250000 0.750000 0.125000 0.625000 0.375000 0.875000 0.062500 0.562500 0.312500
Base 3: 0.333333 0.666667 0.111111 0.444444 0.777778 0.222222 0.555556 0.888889 0.037037 0.370370
Base 4: 0.250000 0.500000 0.750000 0.062500 0.312500 0.562500 0.812500 0.125000 0.375000 0.625000
Base 5: 0.200000 0.400000 0.600000 0.800000 0.040000 0.240000 0.440000 0.640000 0.840000 0.080000
</pre>
=={{header|Scala}}==
<
def compute(n: Int, base: Int = 2) =
Iterator.from(0).
Line 2,315 ⟶ 3,156:
val b = scala.io.StdIn.readInt
(1 to n).foreach(x => println(compute(x, b)))
}</
{{out}}
Line 2,354 ⟶ 3,195:
=={{header|Seed7}}==
{{trans|D}}
<
include "float.s7i";
Line 2,385 ⟶ 3,226:
writeln;
end for;
end func;</
{{out}}
Line 2,405 ⟶ 3,246:
=={{header|Sidef}}==
{{trans|Perl}}
<
while (value[-1] > 0) {
value.append(value[-1] / base -> int)
Line 2,419 ⟶ 3,260:
var seq = 10.of {|i| vdc([i], base) }
"base %d: %s\n".printf(base, seq.map{|n| "%.4f" % n}.join(', '))
}</
{{out}}
<pre>base 2: 0.0000, 0.5000, 0.2500, 0.7500, 0.1250, 0.6250, 0.3750, 0.8750, 0.0625, 0.5625
Line 2,429 ⟶ 3,270:
Stata has builtin functions in Mata to compute '''[https://en.wikipedia.org/wiki/Halton_sequence Halton sequences]''', which are generalizations of the Van der Corput sequence. See '''[https://www.stata.com/help.cgi?mf_halton halton]''' in Stata help, and two articles in the Stata Journal: '''[http://www.stata-journal.com/article.html?article=st0244 Scrambled Halton sequences in Mata]''' by Stanislav Kolenikov and '''[http://www.stata-journal.com/article.html?article=st0103 Generating Halton sequences using Mata]''' by David M. Drukker and Richard Gates.
<
// 5th term of Van der Corput sequence
halton(1,1,5)
Line 2,466 ⟶ 3,307:
+---------------+
end</
Reproduce the plot in the task description:
<
mata
st_addobs(2500)
Line 2,484 ⟶ 3,325:
|| scatter z x, msize(tiny) color(green) legend(off) xtitle("") ///
title(Distribution: Van der Corput (top) vs pseudorandom) ///
ylabel(, angle(0) format(%3.1f))</
=={{header|Swift}}==
{{trans|C}}
<
var n = n, p = 0, q = 1
Line 2,523 ⟶ 3,364:
print()
}</
{{out}}
Line 2,533 ⟶ 3,374:
=={{header|Tcl}}==
The core of this is code to handle digit reversing. Note that this also tackles negative numbers (by preserving the sign independently).
<
set n [expr {[set neg [expr {$n < 0}]] ? -$n : $n}]
set result 0.0
Line 2,542 ⟶ 3,383:
}
return [expr {$neg ? -$result : $result}]
}</
Note that the above procedure will produce terms of the Van der Corput sequence by default.
<
for {set i 1} {$i <= 10} {incr i} {
puts "vanDerCorput($i) = [digitReverse $i]"
Line 2,556 ⟶ 3,397:
}
puts "${base}: [join $seq {, }]"
}</
{{out}}
<pre>
Line 2,575 ⟶ 3,416:
=={{header|VBA}}==
{{trans|Phix}}Base only.<
Dim res As String
Dim digit As Integer, g As Integer, denom As Integer
Line 2,598 ⟶ 3,439:
Debug.Print
Next i
End Sub</
<pre>Base 2 : 0 0.1 0.01 0.11 0.001 0.101 0.011 0.111 0.0001 0.1001
Base 3 : 0 0.1 0.2 0.01 0.11 0.21 0.02 0.12 0.22 0.001
Line 2,605 ⟶ 3,446:
=={{header|VBScript}}==
<
'Van der Corput Sequence fucntion call = VanVanDerCorput(number,base)
Line 2,652 ⟶ 3,493:
RevString = RevString & Mid(s,j,1)
Next
End Function</
{{out}}
<pre>
Line 2,664 ⟶ 3,505:
=={{header|Visual Basic .NET}}==
{{trans|C}}
<
Function ToBase(n As Integer, b As Integer) As String
Line 2,693 ⟶ 3,534:
End Sub
End Module</
{{out}}
<pre>Base = 2
Line 2,706 ⟶ 3,547:
Base = 5
.0 .1 .2 .3 .4 .10 .11 .12 .13 .14 .20 .21 .22</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn v2(nn u32) f64 {
mut n:=nn
mut r := f64(0)
mut p := .5
for n > 0 {
if n&1 == 1 {
r += p
}
p *= .5
n >>= 1
}
return r
}
fn new_v(base u32) fn(u32) f64 {
invb := 1 / f64(base)
return fn[base,invb](nn u32) f64 {
mut n:=nn
mut r := f64(0)
mut p := invb
for n > 0 {
r += p * f64(n%base)
p *= invb
n /= base
}
return r
}
}
fn main() {
println("Base 2:")
for i := u32(0); i < 10; i++ {
println('$i ${v2(i)}')
}
println("Base 3:")
v3 := new_v(3)
for i := u32(0); i < 10; i++ {
println('$i ${v3(i)}')
}
}</syntaxhighlight>
{{out}}
<pre>
Base 2:
0 0
1 0.5
2 0.25
3 0.75
4 0.125
5 0.625
6 0.375
7 0.875
8 0.0625
9 0.5625
Base 3:
0 0
1 0.3333333333333333
2 0.6666666666666666
3 0.1111111111111111
4 0.4444444444444444
5 0.7777777777777777
6 0.2222222222222222
7 0.5555555555555556
8 0.8888888888888888
9 0.037037037037037035
</pre>
=={{header|Wren}}==
{{trans|Go}}
<
var p = 0.5
var r = 0
Line 2,739 ⟶ 3,649:
System.print("\nBase 3:")
var v3 = newV.call(3)
for (i in 0..9) System.print("%(i) -> %(v3.call(i))")</
{{out}}
Line 2,769 ⟶ 3,679:
=={{header|XPL0}}==
<
func real VdC(N); \Return Nth term of van der Corput sequence in base 2
Line 2,784 ⟶ 3,694:
int N;
for N:= 0 to 10-1 do
[IntOut(0, N); RlOut(0, VdC(N)); CrLf(0)]</
{{out}}
<pre>
Line 2,801 ⟶ 3,711:
=={{header|zkl}}==
{{trans|Python}}
<
vdc:=0.0; denom:=1;
while(n){ reg remainder;
Line 2,809 ⟶ 3,719:
}
vdc
}</
{{trans|Ruby}}
<
str:=n.toString(base).reverse();
str.toInt(base).toFloat()/(base.toFloat().pow(str.len()))
}</
{{out}}
<pre>
|