How to resolve the algorithm Van der Corput sequence step by step in the C programming language
How to resolve the algorithm Van der Corput sequence step by step in the C 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 C programming language
The provided C code demonstrates how to convert an integer (in the range 0 to 9) from decimal to any arbitrary base between 2 and 5. It uses a simple algorithm to calculate the numerator and denominator of the fraction representing the value in the new base. Here's a detailed breakdown of the code:
-
Header File Inclusion:
- The code includes the
<stdio.h>
header file for input/output operations likeprintf
.
- The code includes the
-
Function
vc
:-
This function takes four arguments:
n
: The integer to be converted from decimal to the new base.base
: The new base to convert to.*num
: A pointer to an integer where the numerator of the fraction representing the converted value will be stored.*denom
: A pointer to an integer where the denominator of the fraction representing the converted value will be stored.
-
Inside the function, the algorithm to convert
n
to basebase
is implemented as follows:- Initialize
p
to 0 andq
to 1. - While
n
is not 0:- Multiply
p
bybase
and addn % base
to it. - Multiply
q
bybase
. - Divide
n
bybase
.
- Multiply
- Store the calculated
p
in*num
andq
in*denom
. - Perform a GCD (Greatest Common Divisor) calculation on
p
andq
to simplify the fraction. - Divide
*num
and*denom
by the GCD to get the reduced fraction.
- Initialize
-
-
Function
main
:- This is the entry point of the program.
- It iterates through base values from 2 to 5 using a
for
loop. - For each base, it prints the header "base [base]:" and then iterates through decimal values from 0 to 9 using another
for
loop. - For each decimal value, it calls the
vc
function to convert it to the specified base and print the result in the form of a fraction (n/d
) or "0" if the decimal value is 0.
-
Output:
- The program prints the converted values in the specified bases for decimal values from 0 to 9. For example, for
base 2
, it prints:
base 2: 0 0/1 1/1 1/2
- This shows that decimal 0 remains 0 in base 2, decimal 1 becomes 1/1 (same as in decimal), decimal 2 becomes 1/2 in base 2, and so on.
- The program prints the converted values in the specified bases for decimal values from 0 to 9. For example, for
Source code in the c programming language
#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;
}
You may also check:How to resolve the algorithm Strong and weak primes step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Craft Basic programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Wagstaff primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Euphoria programming language