N | PHP 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
< ? 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 ) ; ? >