N | C ++ program to find N - th term in George Cantor set of rational numbers ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }