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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Calkin-Wilf sequence step by step in the Rust 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 Rust programming language

Source code in the rust programming language

// [dependencies]
// num = "0.3"

use num::rational::Rational;

fn calkin_wilf_next(term: &Rational) -> Rational {
    Rational::from_integer(1) / (Rational::from_integer(2) * term.floor() + 1 - term)
}

fn continued_fraction(r: &Rational) -> Vec<isize> {
    let mut a = *r.numer();
    let mut b = *r.denom();
    let mut result = Vec::new();
    loop {
        let (q, r) = num::integer::div_rem(a, b);
        result.push(q);
        a = b;
        b = r;
        if a == 1 {
            break;
        }
    }
    let len = result.len();
    if len != 0 && len % 2 == 0 {
        result[len - 1] -= 1;
        result.push(1);
    }
    result
}

fn term_number(r: &Rational) -> usize {
    let mut result: usize = 0;
    let mut d: usize = 1;
    let mut p: usize = 0;
    for n in continued_fraction(r) {
        for _ in 0..n {
            result |= d << p;
            p += 1;
        }
        d ^= 1;
    }
    result
}

fn main() {
    println!("First 20 terms of the Calkin-Wilf sequence are:");
    let mut term = Rational::from_integer(1);
    for i in 1..=20 {
        println!("{:2}: {}", i, term);
        term = calkin_wilf_next(&term);
    }
    let r = Rational::new(83116, 51639);
    println!("{} is the {}th term of the sequence.", r, term_number(&r));
}


  

You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Slope programming language
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the Datalog programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the XProfan programming language
You may also check:How to resolve the algorithm Last letter-first letter step by step in the BaCon programming language
You may also check:How to resolve the algorithm SHA-256 Merkle tree step by step in the Python programming language