📜  乔治·坎托的第N个有理数集

📅  最后修改于: 2021-05-05 02:04:44             🧑  作者: Mango

乔治·康托尔(George Cantor),一位名叫数学家的数学家,证明了有理数集是可数的。我们不必在这里进行证明,而必须确定有理数集中的第N个项
例子 :

Input : N = 8
Output : 2/3

Input : N = 15
Output : 1/5
See image for reference of counting.

有理数集如下图所示:

乔治·康托

这里第一项为1/1,第二项为1/2,第三项为2/1,第四项为3/1,第五项为2/2,第六项为1/3,依此类推……

C++
// C++ program to find N-th term in
// George Cantor set of rational numbers
#include 
using namespace std;
 
void georgeCantor(int n)
{    
    int i = 1; // let i = numerator
    int j = 1; // let j = denominator
    int k = 1; // to keep the check of no. of terms
         
    // loop till k is not equal to n
    while (k < n)
    {
        j++ , k++;
         
        // check if k is already equal to N
        // then the first term is the required
        // rational number
        if (k == n)
            break;
         
        // loop for traversing from right to left
        // downwards diagonally
        while (j > 1 && k < n) {
            i++, j--, k++;
        }
         
        if (k == n)
           break;
         
        i++, k++;
         
        if (k == n)
           break;
         
        // loop for traversing from left
        // to right upwards diagonally
        while (i > 1 && k < n) {
            i--, j++, k++;
        }       
    }   
    cout << "N-th term : "<


Java
// Java program to find N-th term in
// George Cantor set of rational number
import java.io.*;
 
class GFG
{
    static void georgeCantor(int n)
    {
        // let i = numerator
        int i = 1;
         
        // let j = denominator
        int j = 1;
         
        // to keep the check of no. of terms
        int k = 1;
             
        // loop till k is not equal to n
        while (k < n)
        {
            j++ ;
            k++;
             
            // check if k is already equal to N
            // then the first term is the required
            // rational number
            if (k == n)
                break;
             
            // loop for traversing from right to left
            // downwards diagonally
            while (j > 1 && k < n) {
                i++;
                j--;
                k++;
            }
             
            if (k == n)
            break;
             
            i++;
            k++;
             
            if (k == n)
            break;
             
            // loop for traversing from left
            // to right upwards diagonally
            while (i > 1 && k < n) {
                i--;
                j++;
                k++;
            }
        }
        System.out.println("N-th term : "+i +"/" +j);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 15;
        georgeCantor(n);
         
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 program to find N-th term in
# George Cantor set of rational numbers
 
def georgeCantor(n):
     
    # let i = numerator
    i = 1
     
    # let j = denominator
    j = 1 
     
    # to keep the check of no. of terms
    k = 1 
         
    # loop till k is not equal to n
    while k < n:
        j += 1
        k += 1
         
        # check if k is already equal to N
        # then the first term is the
        # required rational number
        if k == n:
            break
         
        # loop for traversing from right
        # to left downwards diagonally
        while j > 1 and k < n:
            i += 1
            j -= 1
            k += 1
         
        if k == n:
            break
         
        i += 1
        k += 1
         
        if k == n:
            break
         
        # loop for traversing from left
        # to right upwards diagonally
        while i > 1 and k < n:
            i -= 1
            j += 1
            k += 1
    print ("N-th term : %d/%d"%(i, j))
 
# Driver code
n = 15
georgeCantor(n)
 
# This code is contributed
# by Shreyanshi Arun


C#
// C# program to find N-th term in
// George Cantor set of rational number
using System;
 
class GFG
{
    static void georgeCantor(int n)
    {
        // let i = numerator
        int i = 1;
         
        // let j = denominator
        int j = 1;
         
        // to keep the check of no. of terms
        int k = 1;
             
        // loop till k is not equal to n
        while (k < n)
        {
            j++ ;
            k++;
             
            // check if k is already equal to N
            // then the first term is the required
            // rational number
            if (k == n)
                break;
             
            // loop for traversing from right to left
            // downwards diagonally
            while (j > 1 && k < n)
            {
                i++;
                j--;
                k++;
            }
             
            if (k == n)
            break;
             
            i++;
            k++;
             
            if (k == n)
            break;
             
            // loop for traversing from left
            // to right upwards diagonally
            while (i > 1 && k < n)
            {
                i--;
                j++;
                k++;
            }
        }
        Console.WriteLine("N-th term : "+i +"/" +j);
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 15;
        georgeCantor(n);
         
    }
}
 
// This code is contributed by vt_m


PHP
 1 && $k < $n)
        {
            $i++; $j--; $k++;
        }
         
        if ($k == $n)
        break;
         
        $i++; $k++;
         
        if ($k == $n)
        break;
         
        // loop for traversing from left
        // to right upwards diagonally
        while ($i > 1 && $k < $n)
        {
            $i--; $j++; $k++;
        }
    }
    echo "N-th term : ", $i, "/", $j;
}
 
// Driver Code
$n = 15;
georgeCantor($n);
 
// This code is contributed by ajit
?>


Javascript


输出 :

N-th term : 1/5