RosettaCodeData/Task/Van-der-Corput-sequence/00-TASK.txt

57 lines
2.9 KiB
Plaintext

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 digit in the next column to the left 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 also have binary digits to the right of the “point”, just as in the decimal number system. In that 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:
<syntaxhighlight 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]</syntaxhighlight>
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]]
<br><br>