Programa para hallar el N-ésimo término de la serie 3, 5, 33, 35, 53…. – Part 1

Dada una serie de números compuestos únicamente por los dígitos 3 y 5. Los primeros números de la serie son: 
 

3, 5, 33, 35, 53, 55, ….. 
 

Dado un número N. La tarea es encontrar el n-ésimo número en la serie dada.
Ejemplos
 

Input : N = 2
Output : 5

Input : N = 5
Output : 53

La idea se basa en que el valor del último dígito se alterna en la serie. Por ejemplo, si el último dígito del número i es 3, entonces el último dígito de los números (i-1) y ( i +1) debe ser 5. Cree una array de tamaño (n+1) y presione 3 y 5(Estos dos son siempre los dos primeros elementos de la serie) a ella. Para ver más elementos, 

 

1) If i is odd,
      arr[i] = arr[i/2]*10 + 3;
2) If it is even,
      arr[i] = arr[(i/2)-1]*10 + 5;
At last return arr[n].

A continuación se muestra la implementación de la idea anterior: 
 

C++

// C++ program to find n-th number in a series
// made of digits 3 and 5
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find n-th number in series
// made of 3 and 5
int printNthElement(int n)
{
    // create an array of size (n+1)
    int arr[n + 1];
    arr[1] = 3;
    arr[2] = 5;
 
    for (int i = 3; i <= n; i++) {
        // If i is odd
        if (i % 2 != 0)
            arr[i] = arr[i / 2] * 10 + 3;
        else
            arr[i] = arr[(i / 2) - 1] * 10 + 5;
    }
    return arr[n];
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << printNthElement(n);
 
    return 0;
}

C

// C program to find n-th number in a series
// made of digits 3 and 5
#include <stdio.h>
 
// Function to find n-th number in series
// made of 3 and 5
int printNthElement(int n)
{
    // create an array of size (n+1)
    int arr[n + 1];
    arr[1] = 3;
    arr[2] = 5;
 
    for (int i = 3; i <= n; i++) {
        // If i is odd
        if (i % 2 != 0)
            arr[i] = arr[i / 2] * 10 + 3;
        else
            arr[i] = arr[(i / 2) - 1] * 10 + 5;
    }
    return arr[n];
}
 
// Driver code
int main()
{
    int n = 6;
 
    printf("%d",printNthElement(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java program to find n-th number in a series
// made of digits 3 and 5
 
class FindNth {
    // Function to find n-th number in series
    // made of 3 and 5
    static int printNthElement(int n)
    {
        // create an array of size (n+1)
        int arr[] = new int[n + 1];
        arr[1] = 3;
        arr[2] = 5;
 
        for (int i = 3; i <= n; i++) {
            // If i is odd
            if (i % 2 != 0)
                arr[i] = arr[i / 2] * 10 + 3;
            else
                arr[i] = arr[(i / 2) - 1] * 10 + 5;
        }
        return arr[n];
    }
 
    // main function
    public static void main(String[] args)
    {
        int n = 6;
 
        System.out.println(printNthElement(n));
    }
}

Python3

# Python3 program to find n-th number 
# in a series made of digits 3 and 5
   
# Return n-th number in series made 
# of 3 and 5
def printNthElement(n) :
       
    # create an array of size (n + 1)
    arr =[0] * (n + 1);
    arr[1] = 3
    arr[2] = 5
   
    for i in range(3, n + 1) :
        # If i is odd
        if (i % 2 != 0) :
            arr[i] = arr[i // 2] * 10 + 3
        else :
            arr[i] = arr[(i // 2) - 1] * 10 + 5
       
    return arr[n]
       
# Driver code
n = 6
print(printNthElement(n))

C#

// C# program to find n-th number
// in a series made of digits 3 and 5
using System;
 
class GFG
{
// Function to find n-th number
// in series made of 3 and 5
static int printNthElement(int n)
{
    // create an array of size (n+1)
    int [] arr = new int[n + 1];
    arr[1] = 3;
    arr[2] = 5;
 
    for (int i = 3; i <= n; i++)
    {
        // If i is odd
        if (i % 2 != 0)
            arr[i] = arr[i / 2] * 10 + 3;
        else
            arr[i] = arr[(i / 2) - 1] * 10 + 5;
    }
    return arr[n];
}
 
// Driver Code
static void Main()
{
    int n = 6;
 
    Console.WriteLine(printNthElement(n));
}
}
 
// This code is contributed by ANKITRAI1

PHP

<?php
// PHP program to find n-th
// number in a series made
// of digits 3 and 5
 
// Function to find n-th number
// in series made of 3 and 5
function printNthElement($n)
{
    // create an array of size (n+1)
    $arr = array_fill(0, ($n + 1), NULL);
    $arr[1] = 3;
    $arr[2] = 5;
 
    for ($i = 3; $i <= $n; $i++)
    {
        // If i is odd
        if ($i % 2 != 0)
            $arr[$i] = $arr[$i / 2] * 10 + 3;
        else
            $arr[$i] = $arr[($i / 2) -
                             1] * 10 + 5;
    }
    return $arr[$n];
}
 
// Driver code
$n = 6;
 
echo printNthElement($n);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript program to find n-th number in a series
// made of digits 3 and 5
 
    // Function to find n-th number in series
    // made of 3 and 5
    function prletNthElement( n) {
        // create an array of size (n+1)
        let arr = Array(n + 1).fill(0);
        arr[1] = 3;
        arr[2] = 5;
 
        for ( i = 3; i <= n; i++) {
            // If i is odd
            if (i % 2 != 0)
                arr[i] = arr[i / 2] * 10 + 3;
            else
                arr[i] = arr[(i / 2) - 1] * 10 + 5;
        }
        return arr[n];
    }
 
    // main function
      
        let n = 6;
 
        document.write(prletNthElement(n));
 
// This code contributed by Princi Singh
 
</script>
Producción: 

55

 

Publicación traducida automáticamente

Artículo escrito por pawan_asipu 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 *