N | Python3 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
def georgeCantor ( n ) : NEW_LINE INDENT i = 1 NEW_LINE j = 1 NEW_LINE k = 1 NEW_LINE while k < n : NEW_LINE INDENT j += 1 NEW_LINE k += 1 NEW_LINE if k == n : NEW_LINE INDENT break NEW_LINE DEDENT while j > 1 and k < n : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE k += 1 NEW_LINE DEDENT if k == n : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE k += 1 NEW_LINE if k == n : NEW_LINE INDENT break NEW_LINE DEDENT while i > 1 and k < n : NEW_LINE INDENT i -= 1 NEW_LINE j += 1 NEW_LINE k += 1 NEW_LINE DEDENT DEDENT print ( " N - th ▁ term ▁ : ▁ % d / % d " % ( i , j ) ) NEW_LINE DEDENT n = 15 NEW_LINE georgeCantor ( n ) NEW_LINE