How to resolve the algorithm Van der Corput sequence step by step in the Python programming language
How to resolve the algorithm Van der Corput sequence step by step in the Python programming language
Table of Contents
Problem Statement
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
2
0
{\displaystyle 2^{0}}
; the digit in the next column to the left has a multiplier of
2
1
{\displaystyle 2^{1}}
; and so on. So in the following table: the binary number "10" is
1 ×
2
1
0 ×
2
0
{\displaystyle 1\times 2^{1}+0\times 2^{0}}
. 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
2
− 1
{\displaystyle 2^{-1}}
, or
1
/
2
{\displaystyle 1/2}
. The weight for the second column to the right of the point is
2
− 2
{\displaystyle 2^{-2}}
or
1
/
4
{\displaystyle 1/4}
. 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. The third member of the sequence, binary 0.01, is therefore
0 ×
2
− 1
1 ×
2
− 2
{\displaystyle 0\times 2^{-1}+1\times 2^{-2}}
or
1
/
4
{\displaystyle 1/4}
. This sequence is also a superset of the numbers representable by the "fraction" field of 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: the above showing that 11 in decimal is
1 ×
2
3
0 ×
2
2
1 ×
2
1
1 ×
2
0
{\displaystyle 1\times 2^{3}+0\times 2^{2}+1\times 2^{1}+1\times 2^{0}}
. Reflected this would become .1101 or
1 ×
2
− 1
1 ×
2
− 2
0 ×
2
− 3
1 ×
2
− 4
{\displaystyle 1\times 2^{-1}+1\times 2^{-2}+0\times 2^{-3}+1\times 2^{-4}}
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Van der Corput sequence step by step in the Python programming language
The provided Python code defines a function called vdc
that converts an integer n
into its variable-length decimal representation (VD) in the specified base
(defaulting to binary).
-
Function Definition:
def vdc(n, base=2):
This function takes two arguments:
n
: The integer to be converted to VD.base
(optional): The base of the VD representation (defaulting to 2, i.e., binary).
-
VD Calculation Loop: Within a
while
loop, the function calculates the VD representation ofn
one digit at a time:- The variable
denom
keeps track of the denominator (base raised to the power of the current digit position). divmod(n, base)
dividesn
bybase
, returning the quotient (n
) and the remainder (remainder
).remainder / denom
calculates the current VD digit.- The VD digits are accumulated in the
vdc
variable.
- The variable
-
VD Representation: The loop continues until
n
becomes zero, at which point the function returns the calculated VD representation as a floating-point number. -
Function Usage: The code then demonstrates the usage of the
vdc
function:- It calculates and prints the VD representations of the integers from 0 to 9 in binary (base 2).
- It calculates and prints the VD representations of the integers from 0 to 9 in base 3, 4, and 5.
- It uses the
Fraction
class to represent the base as a fraction to achieve arbitrary precision.
-
Output: The output shows the VD representations of the integers in the specified bases:
- In binary (base 2), the VD representations are equivalent to their binary counterparts.
- In higher bases, the VD representations become more complex and non-repeating.
Source code in the python programming language
def vdc(n, base=2):
vdc, denom = 0,1
while n:
denom *= base
n, remainder = divmod(n, base)
vdc += remainder / denom
return vdc
>>> [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]
>>>
>>> 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]
>>> 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]
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Fortran programming language
You may also check:How to resolve the algorithm Write entire file step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Brilliant numbers step by step in the J programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the C++ programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the MiniScript programming language