Encuentra el término N de la serie 1, 6, 18, 40, 75, ….

Dado un número n, la tarea es encontrar el término n-ésimo en la serie 1, 6, 18, 40, 75, …
Ejemplo: 
 

Input: N = 2
Output: 6
Explanation:
2nd term = (2^2*(2+1))/2
         = 6

Input: N = 5
Output: 75
Explanation:
5th term = (5^2*(5+1))/2
         = 75

Acercarse: 
 

N-ésimo término = (N^2*(N+1))/2 
 

La implementación del enfoque anterior se da a continuación: 
 

C++

// CPP code to generate
// 'Nth' term of this sequence
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate a fixed number
int nthTerm(int N)
{
    int nth = 0;
 
    //(N^2*(N+1))/2
    nth = (N * N * (N + 1)) / 2;
 
    return nth;
}
 
// Driver Method
int main()
{
    int N = 5;
 
    cout << nthTerm(N) << endl;
 
    return 0;
}

Java

// Java code to generate
// 'Nth' term of this sequence
 
class GFG {
 
    // Function to generate a fixed number
    public static int nthTerm(int N)
    {
        int nth = 0;
 
        //(N^2*(N+1))/2
        nth = (N * N * (N + 1)) / 2;
 
        return nth;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
        int N = 5;
 
        System.out.println(nthTerm(N));
    }
    // This code is contributed by 29AjayKumar
}

Python3

# python program to find out'Nth' term of this sequence
 
# Function to generate a fixed number
def nthTerm(N):
    nth = 0
    nth = (N * N * (N + 1))//2
    return nth
 
# Driver code
N = 5
print(nthTerm(N))
 
# This code is contributed by Shrikant13

C#

// C# code to generate
// 'Nth' term of this sequence
 
using System;
public class GFG {
 
    // Function to generate a fixed number
    public static int nthTerm(int N)
    {
        int nth = 0;
 
        //(N^2*(N+1))/2
        nth = (N * N * (N + 1)) / 2;
 
        return nth;
    }
 
    // Driver Method
    public static void Main(string[] args)
    {
        int N = 5;
 
        Console.WriteLine(nthTerm(N));
    }
}
 
// This code is contributed by Shrikant13

PHP

<?php
// PHP code to generate
// 'Nth' term of this sequence
 
// Function to generate a fixed number
function nthTerm($N)
{
    $nth = 0;
 
    // (N^2*(N+1))/2
    $nth = ($N * $N * ($N + 1)) / 2;
 
    return $nth;
}
 
// Driver Code
$N = 5;
 
echo nthTerm($N);
 
// This code is contributed
// by chandan_jnu
?>

Javascript

<script>
// Javascript code to generate
// 'Nth' term of this sequence
 
// Function to generate a fixed number
function nthTerm(N)
{
    let nth = 0;
 
    //(N^2*(N+1))/2
    nth = parseInt((N * N * (N + 1)) / 2);
 
    return nth;
}
 
// Driver Method
let N = 5;
 
document.write(nthTerm(N));
 
// This code is contributed by subham348.
</script>
Producción: 

75

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

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