Programa para hallar el término N de la serie 9, 23, 45, 75, 113…

Dado un número N, la tarea es encontrar el N-ésimo término en la serie dada: 
 

9, 23, 45, 75, 113, 159......

Ejemplos: 
 

Input: 4
Output: 113
Explanation:
For N = 4
Nth term = ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N
         = ( 2 * 4 + 3 )*( 2 * 4 + 3 ) - 2 * 4
         = 113

Input: 10
Output: 509

Enfoque: 
El término N de la serie dada se puede generalizar como: 
 

Nth term of the series : ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N

A continuación se muestra la implementación del problema anterior:
Programa: 
 

C++

// CPP program to find N-th term of the series:
// 9, 23, 45, 75, 113...
 
#include <iostream>
using namespace std;
 
// calculate Nth term of series
int nthTerm(int N)
{
    return (2 * N + 3) * (2 * N + 3) - 2 * N;
}
 
// Driver Function
int main()
{
 
    // Get the value of N
    int N = 4;
 
    // Find the Nth term
    // and print it
    cout << nthTerm(N);
 
    return 0;
}

Java

// Java program to find
// N-th term of the series:
// 9, 23, 45, 75, 113...
 
class GFG
{
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (2 * N + 3) *
           (2 * N + 3) - 2 * N;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Get the value of N
    int N = 4;
     
    // Find the Nth term
    // and print it
    System.out.println(nthTerm(N));
}
}
 
// This code is contributed by Bilal

Python3

# Python program to find
# N-th term of the series:
# 9, 23, 45, 75, 113...
def nthTerm(N):
     
    # calculate Nth term of series
    return ((2 * N + 3) *
            (2 * N + 3) - 2 * N);
 
# Driver Code
 
# Get the value of N
n = 4
 
# Find the Nth term
# and print it
print(nthTerm(n))
 
# This code is contributed by Bilal

C#

// C# program to find
// N-th term of the series:
// 9, 23, 45, 75, 113...
using System;
 
class GFG
{
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (2 * N + 3) *
           (2 * N + 3) - 2 * N;
}
 
// Driver code
public static void Main()
{
     
    // Get the value of N
    int N = 4;
     
    // Find the Nth term
    // and print it
    Console.WriteLine(nthTerm(N));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

PHP

<?php
// PHP program to find
// N-th term of the series:
// 9, 23, 45, 75, 113...
 
// calculate Nth term of series
function nthTerm($N)
{
    return (2 * $N + 3) *
           (2 * $N + 3) - 2 * $N;
}
 
// Driver Code
 
// Get the value of N
$N = 4;
 
// Find the Nth term
// and print it
echo nthTerm($N);
 
// This code is contributed by Raj
?>

Javascript

<script>
 
// JavaScript program to find N-th term of the series:
// 9, 23, 45, 75, 113...
 
// calculate Nth term of series
function nthTerm( N)
{
    return (2 * N + 3) * (2 * N + 3) - 2 * N;
}
// Driver Function
 
    // get the value of N
    let N = 4;
 
    // Calculate and print the Nth term
    document.write(  nthTerm(N));
 
// This code is contributed by todaysgaurav
 
</script>
Producción: 

113

 

Complejidad de tiempo: O(1)

Complejidad espacial : O (1) ya que usa variables constantes
 

Publicación traducida automáticamente

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