Related Articles
N-th term of George Cantor set of rational numbers
• Difficulty Level : Easy
• Last Updated : 07 Apr, 2021

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

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
5. After each iteration move to next number to keep counting its position using counter k, then compare it with N

## C++

 `// C++ program to find N-th term in``// George Cantor set of rational numbers``#include ``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 : "``<

## Java

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

## Python3

 `# 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`

## C#

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

## PHP

 ` 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``?>`

## Javascript

 ``

Output :

`N-th term : 1/5`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.72348905,"math_prob":0.964141,"size":6338,"snap":"2021-21-2021-25","text_gpt3_token_len":2050,"char_repetition_ratio":0.13545942,"word_repetition_ratio":0.48770204,"special_character_ratio":0.37440833,"punctuation_ratio":0.1267496,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998822,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-13T23:00:00Z\",\"WARC-Record-ID\":\"<urn:uuid:5753861c-1d63-4c16-b4a8-175337c607b9>\",\"Content-Length\":\"162987\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:387f9a72-a505-460e-9bf6-c6aa02a4cb8a>\",\"WARC-Concurrent-To\":\"<urn:uuid:fe6e1cfe-34c6-4f44-adc4-8ec1edaab6e8>\",\"WARC-IP-Address\":\"23.221.227.248\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/n-th-term-of-george-cantor-set-of-rational-numbers/\",\"WARC-Payload-Digest\":\"sha1:G5CGB25J2YHXNVF7DXPPRGTTKDQBQ66C\",\"WARC-Block-Digest\":\"sha1:DCVOMFW2Y777EB623DK6YPQHXKVZCHJX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243992514.37_warc_CC-MAIN-20210513204127-20210513234127-00437.warc.gz\"}"}