N-th term of George Cantor set of rational numbers
Last Updated :
10 Apr, 2023
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 :
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……
- View this as a matrix(2-D array) with rows from 1 to n and columns from 1 to n.
- rows/columns will give the rational numbers.
- 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.
- There are 4 regular repeating pattern
- After each iteration move to next number to keep counting its position using counter k, then compare it with N
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void georgeCantor( int n)
{
int i = 1;
int j = 1;
int k = 1;
while (k < n)
{
j++ , k++;
if (k == n)
break ;
while (j > 1 && k < n) {
i++, j--, k++;
}
if (k == n)
break ;
i++, k++;
if (k == n)
break ;
while (i > 1 && k < n) {
i--, j++, k++;
}
}
cout << "N-th term : " <<i<< " / " <<j;
}
int main()
{
int n = 15;
georgeCantor(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void georgeCantor( int n)
{
int i = 1 ;
int j = 1 ;
int k = 1 ;
while (k < n)
{
j++ ;
k++;
if (k == n)
break ;
while (j > 1 && k < n) {
i++;
j--;
k++;
}
if (k == n)
break ;
i++;
k++;
if (k == n)
break ;
while (i > 1 && k < n) {
i--;
j++;
k++;
}
}
System.out.println( "N-th term : " +i + "/" +j);
}
public static void main (String[] args)
{
int n = 15 ;
georgeCantor(n);
}
}
|
Python3
def georgeCantor(n):
i = 1
j = 1
k = 1
while k < n:
j + = 1
k + = 1
if k = = n:
break
while j > 1 and k < n:
i + = 1
j - = 1
k + = 1
if k = = n:
break
i + = 1
k + = 1
if k = = n:
break
while i > 1 and k < n:
i - = 1
j + = 1
k + = 1
print ( "N-th term : %d/%d" % (i, j))
n = 15
georgeCantor(n)
|
C#
using System;
class GFG
{
static void georgeCantor( int n)
{
int i = 1;
int j = 1;
int k = 1;
while (k < n)
{
j++ ;
k++;
if (k == n)
break ;
while (j > 1 && k < n)
{
i++;
j--;
k++;
}
if (k == n)
break ;
i++;
k++;
if (k == n)
break ;
while (i > 1 && k < n)
{
i--;
j++;
k++;
}
}
Console.WriteLine( "N-th term : " +i + "/" +j);
}
public static void Main ()
{
int n = 15;
georgeCantor(n);
}
}
|
PHP
<?php
function georgeCantor( $n )
{
$i = 1;
$j = 1;
$k = 1;
while ( $k < $n )
{
$j ++ ; $k ++;
if ( $k == $n )
break ;
while ( $j > 1 && $k < $n )
{
$i ++; $j --; $k ++;
}
if ( $k == $n )
break ;
$i ++; $k ++;
if ( $k == $n )
break ;
while ( $i > 1 && $k < $n )
{
$i --; $j ++; $k ++;
}
}
echo "N-th term : " , $i , "/" , $j ;
}
$n = 15;
georgeCantor( $n );
?>
|
Javascript
<script>
function georgeCantor(n)
{
let i = 1;
let j = 1;
let k = 1;
while (k < n)
{
j++ ;
k++;
if (k == n)
break ;
while (j > 1 && k < n) {
i++;
j--;
k++;
}
if (k == n)
break ;
i++;
k++;
if (k == n)
break ;
while (i > 1 && k < n) {
i--;
j++;
k++;
}
}
document.write( "N-th term : " +i + "/" +j);
}
let n = 15;
georgeCantor(n);
</script>
|
Time Complexity : O(n)
Auxilitary Space Complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...