N-ésimo término del conjunto de números racionales de George Cantor

George Cantor, matemático de nombre, dio una prueba de que el conjunto de números racionales es enumerable. No tenemos que demostrarlo aquí, sino que tenemos que determinar el término N en el conjunto de números racionales.
Ejemplos: 
 

Input : N = 8
Output : 2/3

Input : N = 15
Output : 1/5
See image for reference of counting.

El conjunto de números racionales es como la siguiente imagen:
 

George-Cantor

Aquí el primer término es 1/1, el segundo término es 1/2, el tercer término es 2/1, el cuarto término es 3/1, el quinto término es 2/2, el sexto término es 1/3 y así sucesivamente…
 

1. Vea esto como una array (arreglo 2-D) con filas de 1 a n y columnas de 1 a n.  
2. filas/columnas darán los números racionales. 
3. Observe el patrón de la imagen de arriba, quedará claro cómo atravesar la array, es decir, en diagonal de izquierda a derecha hacia arriba y luego de derecha a izquierda hacia abajo hasta llegar a la posición N.  
4. Hay 4 patrones repetitivos regulares 
5. Después de cada iteración, muévase al siguiente número para seguir contando su posición usando el contador k, luego compárelo con N

C++

// C++ program to find N-th term in
// George Cantor set of rational numbers
#include <bits/stdc++.h>
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 : "<<i<<" / "<<j;
}
 
// driver code
int main()
{
    int n = 15;   
    georgeCantor(n);  
    return 0;
}

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

<?php
// PHP program to find N-th
// term in George Cantor set
// of rational numbers
 
function georgeCantor($n)
{
    $i = 1; // let i = numerator
    $j = 1; // let j = denominator
     
    // to keep the check
    // of no. of terms
    $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++;
        }
    }
    echo "N-th term : ", $i, "/", $j;
}
 
// Driver Code
$n = 15;
georgeCantor($n);
 
// This code is contributed by ajit
?>

Javascript

<script>
// JavaScript program to find N-th term in
// George Cantor set of rational number
 
    function georgeCantor(n)
    {
        // let i = numerator
        let i = 1;
           
        // let j = denominator
        let j = 1;
           
        // to keep the check of no. of terms
        let 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++;
            }
        }
        document.write("N-th term : "+i +"/" +j);
    }
   
// Driver Code
 
        let n = 15;
        georgeCantor(n);
 
// This code is contributed by susmitakundugoaldanga.
</script>

Producción : 
 

N-th term : 1/5

Publicación traducida automáticamente

Artículo escrito por Shashank_Pathak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *