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 counting.

The set of rational numbers goes like the below image :



George-Cantor

Here the first term is 1/1, second term is 1/2, third term is 2/1, fourth term is 3/1, fifth term is 2/2, sixth term is 1/3 so on……

1. View this as a matrix(2-D array) with rows from 1 to n and columns from 1 to n.
2. rows/columns will give the rational numbers.
3. Observe the pattern from the above image it will be clear how to traverse through the matrix i.e diagonally from left to right upwards and then from right to left downwards till we reach the Nth position.
4. There are 4 regular repeating pattern

    One going horizontal one times when i = 1
    Then moving downwards horizontally untill j = 1
    Then moving verticals ones when j = 1
    Then moving diagonal upwards until i = 1

5. After each iteration move to next number to keep counting its position using counter k, then compare it with N

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find N-th term in
// George Cantor set of rational numbers
#include <bits/stdc++.h>
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 : "<<i<<" / "<<j;
}
  
// driver code
int main()
{
    int n = 15;    
    georgeCantor(n);   
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find N-th 
// term in George Cantor set
// of rational numbers
  
function georgeCantor($n)
    $i = 1; // let i = numerator
    $j = 1; // let j = denominator
      
    // to keep the check
    // of no. of terms
    $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++;
        
    
    echo "N-th term : ", $i, "/", $j;
}
  
// Driver Code
$n = 15; 
georgeCantor($n); 
  
// This code is contributed by ajit
?>

chevron_right



Output :

N-th term : 1/5


My Personal Notes arrow_drop_up

Maths is the language of nature

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t