📜  Calkin-Wilf序列中的第n个有理数

📅  最后修改于: 2021-05-25 01:56:47             🧑  作者: Mango

什么是Calkin Wilf序列?
Calkin-Wilf树(或序列)是一种特殊的二叉树,它是通过从分数1/1开始并在每个分数a / b之下迭代地添加a /(a + b)和(a + b)/ b来获得的。该树生成每个有理数。按顺序写出术语将给出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,…序列具有以下特性:每个分母是下一个分子。

上图是列出所有有理数的Calkin-Wilf树。节点a / b的子代计算为a /(a + b)和(a + b)/ b。

任务是在此树的广度优先遍历中找到第n个有理数。
例子:

Input : 13
Output : [5, 3]

Input : 5
Output : [3, 2]

说明:这棵树是一个完美的二叉搜索树,我们需要floor(log(n))步骤来计算第n个有理数。该概念类似于在二叉搜索树中进行搜索。给定n,我们将其一直除以2,直到得到0。我们按以下方式在每个阶段返回分数:

if n%2 == 0
        update frac[1]+=frac[0]
    else
        update frac[0]+=frac[1]

下面是在Calkin Wilf序列中找到第n个数字的程序:

C++
// C++ program to find the 
// nth number in Calkin
// Wilf sequence:
# include
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;
    }
}


Python3
# 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


输出:
[5, 3]

解释:
对于n = 13

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。