nth Rational number in Calkin-Wilf sequence
Last Updated :
07 Jun, 2024
What is Calkin Wilf Sequence?
A Calkin-Wilf tree (or sequence) is a special binary tree which is obtained by starting with the fraction 1/1 and adding a/(a+b) and (a+b)/b iteratively below each fraction a/b. This tree generates every positive rational number. Writing out the terms in a sequence gives 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, ...The sequence has the property that each denominator is the next numerator.

The image above is the Calkin-Wilf Tree where all the rational numbers are listed. The children of a node a/b is calculated as a/(a+b) and (a+b)/b.
The task is to find the nth rational number in breadth first traversal of this tree.
Examples:
Input : 13
Output : [5, 3]
Input : 5
Output : [3, 2]
Explanation:
This tree is a Perfect Binary Search tree and we need floor(log(n)) steps to compute nth rational number. The concept is similar to searching in a binary search tree. Given n we keep dividing it by 2 until we get 0. We return fraction at each stage in the following manner:
if n%2 == 0
update frac[1]+=frac[0]
else
update frac[0]+=frac[1]
Below is the program to find the nth number in Calkin Wilf sequence:
C++
// C++ program to find the
// nth number in Calkin
// Wilf sequence:
# include<bits/stdc++.h>
using namespace std;
int frac[] = {0, 1};
// returns 1x2 int array
// which contains the nth
// rational number
int nthRational(int n)
{
if (n > 0)
nthRational(n / 2);
// ~n&1 is equivalent to
// !n%2?1:0 and n&1 is
// equivalent to n%2
frac[~n & 1] += frac[n & 1];
}
// Driver Code
int main()
{
int n = 13; // testing for n=13
// converting array
// to string format
nthRational(n);
cout << "[" << frac[0] << ","
<< frac[1] << "]" << endl;
return 0;
}
// This code is contributed
// by Harshit Saini
Java
// Java program to find the nth number
// in Calkin Wilf sequence:
import java.util.*;
public class GFG {
static int[] frac = { 0, 1 };
public static void main(String args[])
{
int n = 13; // testing for n=13
// converting array to string format
System.out.println(Arrays.toString(nthRational(n)));
}
// returns 1x2 int array which
// contains the nth rational number
static int[] nthRational(int n)
{
if (n > 0)
nthRational(n / 2);
// ~n&1 is equivalent to !n%2?1:0
// and n&1 is equivalent to n%2
frac[~n & 1] += frac[n & 1];
return frac;
}
}
Python
# Python program to find
# the nth number in Calkin
# Wilf sequence:
frac = [0, 1]
# returns 1x2 int array
# which contains the nth
# rational number
def nthRational(n):
if n > 0:
nthRational(int(n / 2))
# ~n&1 is equivalent to
# !n%2?1:0 and n&1 is
# equivalent to n%2
frac[~n & 1] += frac[n & 1]
return frac
# Driver code
if __name__ == "__main__":
n = 13 # testing for n=13
# converting array
# to string format
print(nthRational(n))
# This code is contributed
# by Harshit Saini
C#
// C# program to find the nth number
// in Calkin Wilf sequence:
using System;
class GFG
{
static int[] frac = { 0, 1 };
public static void Main(String []args)
{
int n = 13; // testing for n=13
// converting array to string format
Console.WriteLine("[" + String.Join(",",
nthRational(n)) + "]");
}
// returns 1x2 int array which
// contains the nth rational number
static int[] nthRational(int n)
{
if (n > 0)
nthRational(n / 2);
// ~n&1 is equivalent to !n%2?1:0
// and n&1 is equivalent to n%2
frac[~n & 1] += frac[n & 1];
return frac;
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to find the nth number
// in Calkin Wilf sequence:
let frac = [0, 1];
let n = 13; // testing for n=13
// converting array to string format
document.write(`[${nthRational(n)}]`)
// returns 1x2 int array which
// contains the nth rational number
function nthRational(n) {
if (n > 0)
nthRational(Math.floor(n / 2));
// ~n&1 is equivalent to !n%2?1:0
// and n&1 is equivalent to n%2
frac[~n & 1] += frac[n & 1];
return frac;
}
// This code is contributed by _saurabh_jaiswal
</script>
Time complexity : O(log(n))
Auxiliary Space : O(1)
Explanation:
For n = 13,

Similar Reads
N-th term of George Cantor set of rational numbers George cantor named mathematician gave a proof that set of rational numbers is enumerable. We don't have to proof it over here rather we have to determine the Nth term in the set of rational numbers. Examples : Input : N = 8 Output : 2/3 Input : N = 15 Output : 1/5 See image for reference of countin
7 min read
Finding nth term of any Polynomial Sequence Given a few terms of a sequence, we are often asked to find the expression for the nth term of this sequence. While there is a multitude of ways to do this, In this article, we discuss an algorithmic approach which will give the correct answer for any polynomial expression. Note that this method fai
4 min read
Find Recurring Sequence in a Fraction Given a fraction, find a recurring sequence of digits if it exists, otherwise, print "No recurring sequence".Examples:Input : Numerator = 8, Denominator = 3Output : Recurring sequence is 6 Explanation : 8/3 = 2.66666666....... Input : Numerator = 50, Denominator = 22Output : Recurring sequence is 27
8 min read
Arithmetic Sequence An arithmetic sequence or progression is defined as a sequence of numbers in which the difference between one term and the next term remains constant.For example: the given below sequence has a common difference of 1.1 2 3 4 5 . . . n â â â â â . . . 1st 2nd 3rd 4th 5th . . . nth TermsThe Arithmetic
8 min read
Nth Fibonacci number using Pell's equation Given an integer N, the task is to find the Nth Fibonacci number. Examples: Input: N = 13 Output: 144 Input: N = 19 Output: 2584 Approach: The Nth Fibonacci number can be found using the roots of the pell's equation. Pells equation is generally of the form (x2) - n(y2) = |1|. Here, consider y2 = x,
4 min read