How to resolve the algorithm Calkin-Wilf sequence step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Calkin-Wilf sequence step by step in the Julia programming language

Table of Contents

Problem Statement

The Calkin-Wilf sequence contains every nonnegative rational number exactly once. It can be calculated recursively as follows:

To avoid floating point error, you may want to use a rational number data type.

It is also possible, given a non-negative rational number, to determine where it appears in the sequence without calculating the sequence. The procedure is to get the continued fraction representation of the rational and use it as the run-length encoding of the binary representation of the term number, beginning from the end of the continued fraction. It only works if the number of terms in the continued fraction is odd- use either of the two equivalent representations to achieve this:

The fraction   9/4   has odd continued fraction representation     2; 3, 1,     giving a binary representation of   100011, which means   9/4   appears as the   35th   term of the sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Calkin-Wilf sequence step by step in the Julia programming language

The provided Julia code is related to the generation and properties of the Calkin-Wilf sequence, a sequence of rational numbers that has interesting mathematical properties. Here's a detailed explanation of the code:

  1. calkin_wilf(n) Function:

    • This function generates the first n terms of the Calkin-Wilf sequence.
    • It initializes an array cw of zeros with a length of n + 1.
    • It iterates through each element i from 2 to n + 1.
    • For each i, it calculates the next term t in the sequence using a formula involving cw[i - 1].
    • It updates cw[i] with the computed t.
    • Finally, it returns the cw array with the first n terms of the Calkin-Wilf sequence (excluding the first element, cw[1], which is always 0).
  2. continued(r::Rational) Function:

    • This function takes a rational number r as input.
    • It calculates the continued fraction expansion of r.
    • It uses an iterative process to generate the coefficients of the continued fraction, which are stored in a vector res.
    • The loop continues until the remainder (a % b) becomes 1, indicating that the continued fraction expansion is complete.
    • It returns the vector res containing the coefficients of the continued fraction expansion.
  3. term_number(cf) Function:

    • This function takes a vector cf of coefficients representing a continued fraction expansion as input.
    • It converts the continued fraction expansion into a binary string.
    • The conversion process involves iterating through the coefficients and constructing a binary string where each coefficient corresponds to a sequence of '0's and '1's.
    • Finally, it parses the binary string and returns the integer value it represents, which corresponds to the term number in the sequence generated by the continued fraction expansion.
  4. Main Code:

    • The code generates the first 20 terms of the Calkin-Wilf sequence using the calkin_wilf function and stores them in the cw constant. It then prints the terms.
    • It defines a constant r with the value 83116.
    • It computes the continued fraction expansion of r using the continued function and stores it in the cf constant.
    • It calculates the term number corresponding to the continued fraction expansion using the term_number function and stores it in the tn constant.
    • Finally, it prints a statement indicating that r is the tn-th term of the sequence.

In summary, this code showcases the use of mathematical functions in Julia to explore the properties of the Calkin-Wilf sequence. It generates the first 20 terms of the sequence, computes the continued fraction expansion of a rational number, and determines the term number corresponding to the continued fraction expansion.

Source code in the julia programming language

function calkin_wilf(n)
    cw = zeros(Rational, n + 1)
    for i in 2:n + 1
        t = Int(floor(cw[i - 1])) * 2 - cw[i - 1] + 1
        cw[i] = 1 // t
    end
    return cw[2:end]
end

function continued(r::Rational)
    a, b = r.num, r.den
    res = []
    while true
        push!(res, Int(floor(a / b)))
        a, b = b, a % b
        a == 1 && break
    end
    return res
end

function term_number(cf)
    b, d = "", "1"
    for n in cf
        b = d^n * b
        d = (d == "1") ? "0" : "1"
    end
    return parse(Int, b, base=2)
end

const cw = calkin_wilf(20)
println("The first 20 terms of the Calkin-Wilf sequence are: $cw")

const r = 83116 // 51639
const cf = continued(r)
const tn = term_number(cf)
println("$r is the $tn-th term of the sequence.")


  

You may also check:How to resolve the algorithm Pell's equation step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Program termination step by step in the Gema programming language
You may also check:How to resolve the algorithm Binary digits step by step in the APL programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the Elixir programming language
You may also check:How to resolve the algorithm Digital root step by step in the Cowgol programming language