Encuentra el término N-ésimo de la serie 2, 15, 41, 80, 132…

Dado un número N , la tarea es encontrar el N- ésimo término de la serie 2, 15, 41, 80, 132… .
Ejemplos: 
 

Entrada: N = 2 
Salida: 15
Entrada: N = 5 
Salida: 132 
 

Enfoque: A partir de la serie dada, la fórmula para el término N se puede encontrar como: 
 

1st term = 2
2nd term = 2 + 1 * 13 = 15
3rd term = 15 + 2 * 13 = 41
4th term = 41 + 3 * 13 = 80
.
.
Nth term = (N - 1)th term
         + (N - 1) * 13

Por lo tanto, el término N de la serie se da de la siguiente manera: 
(N - 1)^{th} \text{término} + (N - 1) * 13
A continuación se muestran los pasos para encontrar el término N mediante la recursividad :
Iteración recursiva desde el valor N
 

  • Caso base: si el valor llamado recursivamente es 1, entonces es el primer término de la serie. Por lo tanto, devuelve 2 de la función. 
     
if(N == 1) 
  return 2;
  • Llamada recursiva: si no se cumple el caso base, iterar recursivamente desde la función de acuerdo con el término N de la serie: 
     
(N - 1) * 13 + func(N - 1);
  • Declaración de devolución: en cada llamada recursiva (excepto el caso base), devuelva la función recursiva para la próxima iteración. 
     
return ((13 * (N - 1))
       + func(N, i + 1));

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to find Nth term
int nthTerm(int N)
{
    // Base Case
    if (N == 1) {
        return 2;
    }
 
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13)
           + nthTerm(N - 1);
}
 
// Driver Code
int main()
{
    // Input Nth term
    int N = 17;
 
    // Function call
    cout << nthTerm(N) << endl;
    return 0;
}

Java

// Java program for the above approach
 
class GFG{
 
// Recursive function to find Nth term
static int nthTerm(int N)
{
    // Base Case
    if (N == 1)
    {
        return 2;
    }
 
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13) +
            nthTerm(N - 1);
}
 
// Driver Code
public static void main(String[] args)
{
    // Input Nth term
    int N = 17;
 
    // Function call
    System.out.print(nthTerm(N) + "\n");
}
}
 
// This code is contributed by 29AjayKumar

Python 3

# Python 3 program for the above approach
 
# Recursive function to find Nth term
def nthTerm(N):
     
    # Base Case
    if (N == 1):
        return 2
 
    # Recursive Call according to
    # Nth term of the series
    return ((N - 1) * 13) + nthTerm(N - 1)
 
# Driver Code
if __name__ == '__main__':
     
    # Input Nth term
    N = 17
 
    # Function call
    print(nthTerm(N))
 
# This code is contributed by Bhupendra_Singh

C#

// C# program for the above approach
using System;
 
public class GFG{
 
// Recursive function to find Nth term
static public int nthTerm(int N)
{
    // Base Case
    if (N == 1)
    {
        return 2;
    }
     
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13) + nthTerm(N - 1);
}
     
// Driver Code
static public void Main ()
{
    // Input Nth term
    int N = 17;
     
    // Function call
    Console.WriteLine(nthTerm(N));
}
}
 
//This code is contributed by shubhamsingh10

Javascript

<script>
// javascript program for the above approach
 
// Recursive function to find Nth term
function nthTerm( N)
{
 
    // Base Case
    if (N == 1) {
        return 2;
    }
 
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13)
           + nthTerm(N - 1);
}
 
// Driver Code
 
    // Input Nth term
    let N = 17;
 
    // Function call
   document.write( nthTerm(N) );
    
// This code is contributed by Rajput-Ji
 
</script>
Producción

1770

Programación dinámica (de arriba hacia abajo usando memorización):

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
int static dp[1001];
 
// function to find Nth term
int nthTerm(int N)
{
    // Base Case
    if (N == 1) {
        return 2;
    }
     
    // If a value already present,
    // return it
    if(dp[N] != -1) {
        return dp[N];
    }
    // Recursive Call according to
    // Nth term of the series and
    // store it in dp
    dp[N] = ((N - 1) * 13) + nthTerm(N - 1);
     
    // Return dp
    return dp[N];
}
 
// Driver Code
int main()
{
    // Input Nth term
    int N = 17;
 
    memset(dp, -1, sizeof(dp));
     
    // Function call
    cout << nthTerm(N) << endl;
    return 0;
}
// This code is contributed by Samim Hossain Mondal.

Java

// Java implementation of above approach
import java.util.*;
public class GFG{
 
  static int []dp = new int[1001];
 
  // function to find Nth term
  static int nthTerm(int N)
  {
    // Base Case
    if (N == 1) {
      return 2;
    }
 
    // If a value already present,
    // return it
    if(dp[N] != -1) {
      return dp[N];
    }
    // Recursive Call according to
    // Nth term of the series and
    // store it in dp
    dp[N] = ((N - 1) * 13) + nthTerm(N - 1);
 
    // Return dp
    return dp[N];
  }
 
  // Driver code
  public static void main(String []args)
  {
    // Input Nth term
    int N = 17;
 
    for(int i = 0; i < 1001; i++) {
      dp[i] = -1;
    }
 
    System.out.println(nthTerm(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Python

# Pythonnprogram for the above approach
 
# Taking the matrix as globally
dp = [-1 for i in range(1001)]
 
# Function to find Nth term
def nthTerm(N):
     
    # Base Case
    if (N == 1):
        return 2
 
    # If a value already present,
    # return it
    if(dp[N] != -1):
        return dp[N]
         
    # Recursive Call according to
    # Nth term of the series and
    # store it in dp
    dp[N] = ((N - 1) * 13) + nthTerm(N - 1)
     
    # Return dp
    return dp[N]
 
# Driver Code
if __name__ == '__main__':
     
    # Input Nth term
    N = 17
 
    # Function call
    print(nthTerm(N))
 
# This code is contributed by Samim Hossain Mondal.

C#

// C# implementation of above approach
using System;
class GFG
{
  static int []dp = new int[1001];
 
  // function to find Nth term
  static int nthTerm(int N)
  {
 
    // Base Case
    if (N == 1) {
      return 2;
    }
 
    // If a value already present,
    // return it
    if(dp[N] != -1) {
      return dp[N];
    }
 
    // Recursive Call according to
    // Nth term of the series and
    // store it in dp
    dp[N] = ((N - 1) * 13) + nthTerm(N - 1);
 
    // Return dp
    return dp[N];
  }
 
  // Driver code
  public static void Main()
  {
 
    // Input Nth term
    int N = 17;
 
    for(int i = 0; i < 1001; i++) {
      dp[i] = -1;
    }
 
    Console.Write(nthTerm(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript

<script>
// Javascript program for the above approach
 
let dp = [];
 
// function to find Nth term
function nthTerm(N)
{
    // Base Case
    if (N == 1) {
        return 2;
    }
     
    // If a value already present,
    // return it
    if(dp[N] != -1) {
        return dp[N];
    }
    // Recursive Call according to
    // Nth term of the series and
    // store it in dp
    dp[N] = ((N - 1) * 13) + nthTerm(N - 1);
     
    // Return dp
    return dp[N];
}
 
// Driver Code
 
// Input Nth term
let N = 17;
 
for(let i = 0; i < 1001; i++) {
    dp[i] = -1;
}
 
// Function call
document.write(nthTerm(N));
 
// This code is contributed by Samim Hossain Mondal.
</script>
Producción

1770

Complejidad de tiempo: O(N)

Espacio Auxiliar: O(N)

Publicación traducida automáticamente

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