all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
51
Task/Van-der-Corput-sequence/0DESCRIPTION
Normal file
51
Task/Van-der-Corput-sequence/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
When counting integers in binary, if you put a (binary) point to the right of the count then the column immediately to the left denotes a digit with a multiplier of <math>2^0</math>; the next column to the lefts digit has a multiplier of <math>2^1</math> and so on.
|
||||
|
||||
So in the following table:
|
||||
<pre> 0.
|
||||
1.
|
||||
10.
|
||||
11.
|
||||
...</pre>
|
||||
The binary number "<code>10</code>" is <math>1 \times 2^1 + 0 \times 2^0</math>.
|
||||
|
||||
You can have binary digits to the right of the “point” just as in the decimal number system too. in this case, the digit in the place immediately to the right of the point has a weight of <math>2^{-1}</math>, or <math>1/2</math>. The weight for the second column to the right of the point is <math>2^{-2}</math> or <math>1/4</math>. And so on.
|
||||
|
||||
If you take the integer binary count of the first table, and ''reflect'' the digits about the binary point, you end up with '''the van der Corput sequence of numbers in base 2'''.
|
||||
|
||||
<pre> .0
|
||||
.1
|
||||
.01
|
||||
.11
|
||||
...</pre>
|
||||
|
||||
The third member of the sequence: binary <code>0.01</code> is therefore <math>0 \times 2^{-1} + 1 \times 2^{-2}</math> or <math>1/4</math>.
|
||||
|
||||
<br> [[File:Van der corput distribution.png|400|thumb|right|Distribution of 2500 points each: Van der Corput (top) vs pseudorandom]] Members of the sequence lie within the interval <math>0 \leq x < 1</math>. Points within the sequence tend to be evenly distributed which is a useful trait to have for [[wp:Monte Carlo method|Monte Carlo simulations]].
|
||||
This sequence is also a superset of the numbers representable by the "fraction" field of [[wp:IEEE 754-1985|an old IEEE floating point standard]]. In that standard, the "fraction" field represented the fractional part of a binary number beginning with "1." e.g. 1.101001101.
|
||||
|
||||
'''Hint'''
|
||||
|
||||
A ''hint'' at a way to generate members of the sequence is to modify a routine used to change the base of an integer:
|
||||
<lang python>>>> def base10change(n, base):
|
||||
digits = []
|
||||
while n:
|
||||
n,remainder = divmod(n, base)
|
||||
digits.insert(0, remainder)
|
||||
return digits
|
||||
|
||||
>>> base10change(11, 2)
|
||||
[1, 0, 1, 1]</lang>
|
||||
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>
|
||||
|
||||
'''Task Description'''
|
||||
|
||||
* Create a function/method/routine that given ''n'', generates the ''n'''th term of the van der Corput sequence in base 2.
|
||||
* Use the function to compute ''and display'' the first ten members of the sequence. (The first member of the sequence is for ''n''=0).
|
||||
|
||||
* As a stretch goal/extra credit, compute and show members of the sequence for bases other than 2.
|
||||
|
||||
''See also''
|
||||
* [http://www.puc-rio.br/marco.ind/quasi_mc.html#low_discrep The Basic Low Discrepancy Sequences]
|
||||
* [[Non-decimal radices/Convert]]
|
||||
* [[wp:Van der Corput sequence|Van der Corput sequence]]
|
||||
27
Task/Van-der-Corput-sequence/Ada/van-der-corput-sequence.ada
Normal file
27
Task/Van-der-Corput-sequence/Ada/van-der-corput-sequence.ada
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Main is
|
||||
package Float_IO is new Ada.Text_IO.Float_IO (Float);
|
||||
function Van_Der_Corput (N : Natural; Base : Positive := 2) return Float is
|
||||
Value : Natural := N;
|
||||
Result : Float := 0.0;
|
||||
Exponent : Positive := 1;
|
||||
begin
|
||||
while Value > 0 loop
|
||||
Result := Result +
|
||||
Float (Value mod Base) / Float (Base ** Exponent);
|
||||
Value := Value / Base;
|
||||
Exponent := Exponent + 1;
|
||||
end loop;
|
||||
return Result;
|
||||
end Van_Der_Corput;
|
||||
begin
|
||||
for Base in 2 .. 5 loop
|
||||
Ada.Text_IO.Put ("Base" & Integer'Image (Base) & ":");
|
||||
for N in 1 .. 10 loop
|
||||
Ada.Text_IO.Put (' ');
|
||||
Float_IO.Put (Item => Van_Der_Corput (N, Base), Exp => 0);
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
end Main;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
@% = &20509
|
||||
FOR base% = 2 TO 5
|
||||
PRINT "Base " ; STR$(base%) ":"
|
||||
FOR number% = 0 TO 9
|
||||
PRINT FNvdc(number%, base%);
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT
|
||||
END
|
||||
|
||||
DEF FNvdc(n%, b%)
|
||||
LOCAL v, s%
|
||||
s% = 1
|
||||
WHILE n%
|
||||
s% *= b%
|
||||
v += (n% MOD b%) / s%
|
||||
n% DIV= b%
|
||||
ENDWHILE
|
||||
= v
|
||||
26
Task/Van-der-Corput-sequence/C++/van-der-corput-sequence.cpp
Normal file
26
Task/Van-der-Corput-sequence/C++/van-der-corput-sequence.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
double vdc(int n, double base = 2)
|
||||
{
|
||||
double vdc = 0, denom = 1;
|
||||
while (n)
|
||||
{
|
||||
vdc += fmod(n, base) / (denom *= base);
|
||||
n /= base; // note: conversion from 'double' to 'int'
|
||||
}
|
||||
return vdc;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
for (double base = 2; base < 6; ++base)
|
||||
{
|
||||
std::cout << "Base " << base << "\n";
|
||||
for (int n = 0; n < 10; ++n)
|
||||
{
|
||||
std::cout << vdc(n, base) << " ";
|
||||
}
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
}
|
||||
35
Task/Van-der-Corput-sequence/C/van-der-corput-sequence-1.c
Normal file
35
Task/Van-der-Corput-sequence/C/van-der-corput-sequence-1.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void vc(int n, int base, int *num, int *denom)
|
||||
{
|
||||
int p = 0, q = 1;
|
||||
|
||||
while (n) {
|
||||
p = p * base + (n % base);
|
||||
q *= base;
|
||||
n /= base;
|
||||
}
|
||||
|
||||
*num = p;
|
||||
*denom = q;
|
||||
|
||||
while (p) { n = p; p = q % p; q = n; }
|
||||
*num /= q;
|
||||
*denom /= q;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int d, n, i, b;
|
||||
for (b = 2; b < 6; b++) {
|
||||
printf("base %d:", b);
|
||||
for (i = 0; i < 10; i++) {
|
||||
vc(i, b, &n, &d);
|
||||
if (n) printf(" %d/%d", n, d);
|
||||
else printf(" 0");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(defun van-der-Corput (n base)
|
||||
(loop for d = 1 then (* d base) while (<= d n)
|
||||
finally
|
||||
(return (/ (parse-integer
|
||||
(reverse (write-to-string n :base base))
|
||||
:radix base)
|
||||
d))))
|
||||
|
||||
(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))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16 5/16
|
||||
Base 3: 0 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27 10/27
|
||||
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
|
||||
16
Task/Van-der-Corput-sequence/D/van-der-corput-sequence.d
Normal file
16
Task/Van-der-Corput-sequence/D/van-der-corput-sequence.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
double vdc(int n, in double base=2.0) pure nothrow {
|
||||
double vdc = 0.0, denom = 1.0;
|
||||
while (n) {
|
||||
denom *= base;
|
||||
vdc += (n % base) / denom;
|
||||
n /= base;
|
||||
}
|
||||
return vdc;
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (b; 2 .. 6)
|
||||
writeln("\nBase ", b, ": ", iota(10).map!(n => vdc(n, b))());
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
open random number list
|
||||
|
||||
vdc bs n = vdc' 0.0 1.0 n
|
||||
where vdc' v d n
|
||||
| n > 0 = vdc' v' d' n'
|
||||
| else = v
|
||||
where
|
||||
d' = d * bs
|
||||
rem = n % bs
|
||||
n' = truncate (n / bs)
|
||||
v' = v + rem / d'
|
||||
|
|
@ -0,0 +1 @@
|
|||
take 10 <| map' (vdc 2.0) [1..]
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function vdc(integer n, atom base)
|
||||
atom vdc, denom, rem
|
||||
vdc = 0
|
||||
denom = 1
|
||||
while n do
|
||||
denom *= base
|
||||
rem = remainder(n,base)
|
||||
n = floor(n/base)
|
||||
vdc += rem / denom
|
||||
end while
|
||||
return vdc
|
||||
end function
|
||||
|
||||
for i = 2 to 5 do
|
||||
printf(1,"Base %d\n",i)
|
||||
for j = 0 to 9 do
|
||||
printf(1,"%g ",vdc(j,i))
|
||||
end for
|
||||
puts(1,"\n\n")
|
||||
end for
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
: fvdc ( base n -- f )
|
||||
0e 1e ( F: vdc denominator )
|
||||
begin dup while
|
||||
over s>d d>f f*
|
||||
over /mod ( base rem n )
|
||||
swap s>d d>f fover f/
|
||||
frot f+ fswap
|
||||
repeat 2drop fdrop ;
|
||||
|
||||
: test 10 0 do 2 i fvdc cr f. loop ;
|
||||
40
Task/Van-der-Corput-sequence/Go/van-der-corput-sequence.go
Normal file
40
Task/Van-der-Corput-sequence/Go/van-der-corput-sequence.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func v2(n uint) (r float64) {
|
||||
p := .5
|
||||
for n > 0 {
|
||||
if n&1 == 1 {
|
||||
r += p
|
||||
}
|
||||
p *= .5
|
||||
n >>= 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func newV(base uint) func(uint) float64 {
|
||||
invb := 1 / float64(base)
|
||||
return func(n uint) (r float64) {
|
||||
p := invb
|
||||
for n > 0 {
|
||||
r += p * float64(n%base)
|
||||
p *= invb
|
||||
n /= base
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Base 2:")
|
||||
for i := uint(0); i < 10; i++ {
|
||||
fmt.Println(i, v2(i))
|
||||
}
|
||||
fmt.Println("Base 3:")
|
||||
v3 := newV(3)
|
||||
for i := uint(0); i < 10; i++ {
|
||||
fmt.Println(i, v3(i))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
import Data.List
|
||||
import Data.Ratio
|
||||
import System.Environment
|
||||
import Text.Printf
|
||||
|
||||
-- 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) ++ "/" ++ show (denominator 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 0 = Nothing
|
||||
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 < 2 = error "vdc: base must be ≥ 2"
|
||||
| otherwise = let ds = reverse $ numToDigits b n
|
||||
in Rat (digitsToNum b ds % b ^ length ds)
|
||||
|
||||
-- Print the base followed by a sequence of van der Corput numbers.
|
||||
printVdc :: (Integer,[Rat]) -> IO ()
|
||||
printVdc (b,ns) = putStrLn $ printf "Base %d:" b
|
||||
++ concatMap (printf " %5s" . show) ns
|
||||
|
||||
-- To print the n'th van der Corput numbers for n in [2,3,4,5] call the program
|
||||
-- with no arguments. Otherwise, passing the base b, first n, next n and
|
||||
-- maximum n will print the base b numbers for n in [firstN, nextN, ..., maxN].
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
let (bases, nums) = case args of
|
||||
[b, f, s, m] -> ([read b], [read f, read s..read m])
|
||||
_ -> ([2,3,4,5], [0..9])
|
||||
mapM_ printVdc [(b,rs) | b <- bases, let rs = map (vdc b) nums]
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
procedure main(A)
|
||||
base := integer(get(A)) | 2
|
||||
every writes(round(vdc(0 to 9,base),10)," ")
|
||||
write()
|
||||
end
|
||||
|
||||
procedure vdc(n, base)
|
||||
e := 1.0
|
||||
x := 0.0
|
||||
while x +:= 1(((0 < n) % base) / (e *:= base), n /:= base)
|
||||
return x
|
||||
end
|
||||
|
||||
procedure round(n,d)
|
||||
places := 10 ^ d
|
||||
return real(integer(n*places + 0.5)) / places
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
procedure vdc(n, base)
|
||||
s1 := create |((0 < 1(.n, n /:= base)) % base)
|
||||
s2 := create 2(e := 1.0, |(e *:= base))
|
||||
every (result := 0) +:= |s1() / s2()
|
||||
return result
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
vdc=: ([ %~ %@[ #. #.inv)"0 _
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
2 vdc i.10 NB. 1st 10 nums of Van der Corput sequence in base 2
|
||||
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
|
||||
0 1r2 1r4 3r4 1r8 5r8 3r8 7r8 1r16 9r16
|
||||
2 3 4 5x vdc i.10 NB. 1st 10 nums of Van der Corput sequence in bases 2 3 4 5
|
||||
0 1r2 1r4 3r4 1r8 5r8 3r8 7r8 1r16 9r16
|
||||
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
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
public class VanDerCorput{
|
||||
public static double vdc(int n){
|
||||
double vdc = 0;
|
||||
int denom = 1;
|
||||
while(n != 0){
|
||||
vdc += n % 2.0 / (denom *= 2);
|
||||
n /= 2;
|
||||
}
|
||||
return vdc;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
for(int i = 0; i <= 10; i++){
|
||||
System.out.println(vdc(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function x = corput (n)
|
||||
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;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
VanDerCorput[n_,base_:2]:=Table[
|
||||
FromDigits[{Reverse[IntegerDigits[k,base]],0},base],
|
||||
{k,n}]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
VdC(n)=n=binary(n);sum(i=1,#n,if(n[i],1.>>(#n+1-i)));
|
||||
VdC(n)=sum(i=1,#binary(n),if(bittest(n,i-1),1.>>i)); \\ Alternate approach
|
||||
vector(10,n,VdC(n))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
vdcb: procedure (an) returns (bit (31)); /* 6 July 2012 */
|
||||
declare an fixed binary (31);
|
||||
declare (n, i) fixed binary (31);
|
||||
declare v bit (31) varying;
|
||||
|
||||
n = an; v = ''b;
|
||||
do i = 1 by 1 while (n > 0);
|
||||
if iand(n, 1) = 1 then v = v || '1'b; else v = v || '0'b;
|
||||
n = isrl(n, 1);
|
||||
end;
|
||||
return (v);
|
||||
end vdcb;
|
||||
|
||||
declare i fixed binary (31);
|
||||
|
||||
do i = 0 to 10;
|
||||
put skip list ('0.' || vdcb(i));
|
||||
end;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
sub vdc($num, $base = 2) {
|
||||
my $n = $num;
|
||||
my $vdc = 0;
|
||||
my $denom = 1;
|
||||
while $n {
|
||||
$vdc += $n mod $base / ($denom *= $base);
|
||||
$n div= $base;
|
||||
}
|
||||
$vdc;
|
||||
}
|
||||
|
||||
for 2..5 -> $b {
|
||||
say "Base $b";
|
||||
say (vdc($_,$b) for ^10).perl;
|
||||
say '';
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
sub vdc($value, $base = 2) {
|
||||
my @values := $value, { $_ div $base } ... 0;
|
||||
my @denoms := $base, { $_ * $base } ... *;
|
||||
[+] do for @values Z @denoms -> $v, $d {
|
||||
$v mod $base / $d;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(scl 6)
|
||||
|
||||
(de vdc (N B)
|
||||
(default B 2)
|
||||
(let (R 0 A 1.0)
|
||||
(until (=0 N)
|
||||
(inc 'R (* (setq A (/ A B)) (% N B)))
|
||||
(setq N (/ N B)) )
|
||||
R ) )
|
||||
|
||||
(for B (2 3 4)
|
||||
(prinl "Base: " B)
|
||||
(for N (range 0 9)
|
||||
(prinl N ": " (round (vdc N B) 4)) ) )
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
% vdc( N, Base, Out )
|
||||
% Out = the Van der Corput representation of N in given Base
|
||||
vdc( 0, _, [] ).
|
||||
vdc( N, Base, Out ) :-
|
||||
Nr is mod(N, Base),
|
||||
Nq is N // Base,
|
||||
vdc( Nq, Base, Tmp ),
|
||||
Out = [Nr|Tmp].
|
||||
|
||||
% Writes every element of a list to stdout; no newlines
|
||||
write_list( [] ).
|
||||
write_list( [H|T] ) :-
|
||||
write( H ),
|
||||
write_list( T ).
|
||||
|
||||
% Writes the Nth Van der Corput item.
|
||||
print_vdc( N, Base ) :-
|
||||
vdc( N, Base, Lst ),
|
||||
write('0.'),
|
||||
write_list( Lst ).
|
||||
print_vdc( N ) :-
|
||||
print_vdc( N, 2 ).
|
||||
|
||||
% Prints the first N+1 elements of the Van der Corput
|
||||
% sequence, each to its own line
|
||||
print_some( 0, _ ) :-
|
||||
write( '0.0' ).
|
||||
print_some( N, Base ) :-
|
||||
M is N - 1,
|
||||
print_some( M, Base ),
|
||||
nl,
|
||||
print_vdc( N, Base ).
|
||||
print_some( N ) :-
|
||||
print_some( N, 2 ).
|
||||
|
||||
test :-
|
||||
writeln('First 10 members in base 2:'),
|
||||
print_some( 9 ),
|
||||
nl,
|
||||
write('7th member in base 4 (stretch goal) => '),
|
||||
print_vdc( 7, 4 ).
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def vdc(n, base=2):
|
||||
vdc, denom = 0,1
|
||||
while n:
|
||||
denom *= base
|
||||
n, remainder = divmod(n, base)
|
||||
vdc += remainder / denom
|
||||
return vdc
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>>> [vdc(i) for i in range(10)]
|
||||
[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]
|
||||
>>>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
>>> from fractions import 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]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
>>> for b in range(3,6):
|
||||
print('\nBase', b)
|
||||
print([vdc(i, base=Fraction(b)) for i in range(10)])
|
||||
|
||||
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]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/*REXX pgm converts any integer (or a range) to a van der Corput number in base 2*/
|
||||
/*convert any integer (or a range) to a van der Corput number in base 2*/
|
||||
|
||||
numeric digits 1000 /*handle anything the user wants.*/
|
||||
parse arg a b . /*obtain the number(s) [maybe]. */
|
||||
if a=='' then do; a=0; b=10; end /*if none specified, use defaults*/
|
||||
if b=='' then b=a /*assume a "range" of a single #.*/
|
||||
|
||||
do j=a to b /*traipse through the range. */
|
||||
_=vdC(abs(j)) /*convert abs value of integer. */
|
||||
leading=substr('-',2+sign(j)) /*if needed, leading minus sign. */
|
||||
say leading || _ /*show number (with leading - ?)*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────VDC [van der Corput] subroutine─────*/
|
||||
vdC: procedure; y=x2b(d2x(arg(1))) /*convert to hex, then binary. */
|
||||
if y=0 then return 0 /*zero has been converted to 0000*/
|
||||
else return '.'reverse(strip(y,"L",0)) /*heavy lifting by REXX*/
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*REXX pgm converts any integer (or a range) to a van der Corput number in base 2,*/
|
||||
/* or optionaly, any other base up to base 90. */
|
||||
/*If the base is negative, the output is shown as a list. */
|
||||
|
||||
numeric digits 1000 /*handle anything the user wants.*/
|
||||
parse arg a b r . /*obtain the number(s) [maybe]. */
|
||||
if a=='' then do; a=0; b=10; end /*if none specified, use defaults*/
|
||||
if b=='' then b=a /*assume a "range" of a single #.*/
|
||||
if r=='' then r=2 /*assume a radix (base) of 2. */
|
||||
z= /*placeholder for a list of nums.*/
|
||||
|
||||
do j=a to b /*traipse through the range. */
|
||||
_=vdC(abs(j),abs(r)) /*convert abs value of integer. */
|
||||
_=substr('-',2+sign(j))_ /*if needed, leading minus sign. */
|
||||
if r>0 then say _ /*if positive base, just show it.*/
|
||||
else z=z _ /* ... else build a list. */
|
||||
end /*j*/
|
||||
|
||||
if z\=='' then say strip(z) /*if list wanted, then show it. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────VDC [van der Corput] subroutine─────*/
|
||||
vdC: return '.'reverse(base(arg(1),arg(2))) /*convert, reverse, append.*/
|
||||
/*──────────────────────────────────BASE subroutine (up to base 90)─────*/
|
||||
base: procedure; parse arg x,toB,inB /*get a number, toBase, inBase */
|
||||
|
||||
/*┌────────────────────────────────────────────────────────────────────┐
|
||||
┌─┘ Input to this subroutine (all must be positive whole numbers): └─┐
|
||||
│ │
|
||||
│ x (is required). │
|
||||
│ toBase the base to convert X to. │
|
||||
│ inBase the base X is expressed in. │
|
||||
│ │
|
||||
│ toBase or inBase can be omitted which causes the default of │
|
||||
└─┐ 10 to be used. The limits of both are: 2 ──► 90. ┌─┘
|
||||
└────────────────────────────────────────────────────────────────────┘*/
|
||||
|
||||
@abc='abcdefghijklmnopqrstuvwxyz' /*random characters, sorted. */
|
||||
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
|
||||
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/
|
||||
@@@=@@@'<>[]{}()?~!@#$%^&*_+-=|\/;:~' /*add some special chars as well.*/
|
||||
/*spec. chars should be viewable.*/
|
||||
numeric digits 1000 /*what the hey, support biggies. */
|
||||
maxB=length(@@@) /*max base (radix) supported here*/
|
||||
parse arg x,toB,inB /*get a number, toBase, inBase */
|
||||
if toB=='' then toB=10 /*if skipped, assume default (10)*/
|
||||
if inB=='' then inB=10 /* " " " " " */
|
||||
|
||||
/*══════════════════════════════════convert X from base inB ──► base 10.*/
|
||||
#=0
|
||||
do j=1 for length(x)
|
||||
_=substr(x,j,1) /*pick off a "digit" from X. */
|
||||
v=pos(_,@@@) /*get the value of this "digits".*/
|
||||
if v==0 |v>inB then call erd x,j,inB /*illegal "digit"? */
|
||||
#=#*inB+v-1 /*construct new num, dig by dig. */
|
||||
end /*j*/
|
||||
|
||||
/*══════════════════════════════════convert # from base 10 ──► base toB.*/
|
||||
y=
|
||||
do while #>=toB /*deconstruct the new number (#).*/
|
||||
y=substr(@@@,(#//toB)+1,1)y /*construnct the output number. */
|
||||
#=#%toB /*... and whittle # down also. */
|
||||
end /*while*/
|
||||
|
||||
return substr(@@@,#+1,1)y
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
def vdc(n, base=2)
|
||||
str = n.to_s(base).reverse
|
||||
str.to_i(base).quo(base ** str.length)
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> Array.new(10){|i| vdc(i,3)}
|
||||
=> [Rational(0, 1), Rational(1, 3), Rational(2, 3), Rational(1, 9), Rational(4, 9), Rational(7, 9), Rational(2, 9), Rational(5, 9), Rational(8, 9), Rational(1, 27)]
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "float.s7i";
|
||||
|
||||
const func float: vdc (in var integer: number, in integer: base) is func
|
||||
result
|
||||
var float: vdc is 0.0;
|
||||
local
|
||||
var integer: denom is 1;
|
||||
var integer: remainder is 0;
|
||||
begin
|
||||
while number <> 0 do
|
||||
denom *:= base;
|
||||
remainder := number rem base;
|
||||
number := number div base;
|
||||
vdc +:= flt(remainder) / flt(denom);
|
||||
end while;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: base is 0;
|
||||
var integer: number is 0;
|
||||
begin
|
||||
for base range 2 to 5 do
|
||||
writeln;
|
||||
writeln("Base " <& base);
|
||||
for number range 0 to 9 do
|
||||
write(vdc(number, base) digits 6 <& " ");
|
||||
end for;
|
||||
writeln;
|
||||
end for;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
proc digitReverse {n {base 2}} {
|
||||
set n [expr {[set neg [expr {$n < 0}]] ? -$n : $n}]
|
||||
set result 0.0
|
||||
set bit [expr {1.0 / $base}]
|
||||
for {} {$n > 0} {set n [expr {$n / $base}]} {
|
||||
set result [expr {$result + $bit * ($n % $base)}]
|
||||
set bit [expr {$bit / $base}]
|
||||
}
|
||||
return [expr {$neg ? -$result : $result}]
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
# Print the first 10 terms of the Van der Corput sequence
|
||||
for {set i 1} {$i <= 10} {incr i} {
|
||||
puts "vanDerCorput($i) = [digitReverse $i]"
|
||||
}
|
||||
|
||||
# In other bases
|
||||
foreach base {3 4 5} {
|
||||
set seq {}
|
||||
for {set i 1} {$i <= 10} {incr i} {
|
||||
lappend seq [format %.5f [digitReverse $i $base]]
|
||||
}
|
||||
puts "${base}: [join $seq {, }]"
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
|
||||
func real VdC(N); \Return Nth term of van der Corput sequence in base 2
|
||||
int N;
|
||||
real V, U;
|
||||
[V:= 0.0; U:= 0.5;
|
||||
repeat N:= N/2;
|
||||
if rem(0) then V:= V+U;
|
||||
U:= U/2.0;
|
||||
until N=0;
|
||||
return V;
|
||||
];
|
||||
|
||||
int N;
|
||||
for N:= 0 to 10-1 do
|
||||
[IntOut(0, N); RlOut(0, VdC(N)); CrLf(0)]
|
||||
Loading…
Add table
Add a link
Reference in a new issue