Find the Nth term of the series 2 + 6 + 13 + 23 + . . .

Given an integer N. The task is to write a program to find the Nth term of the given series: 
 

2 + 6 + 13 + 23 + … 

Examples
 

Input : N = 5
Output : 36

Input : N = 10
Output : 146

 

Refer the article on How to find Nth term of series to know idea behind finding Nth term of any given series.
The generalized N-th term of given series is: 
t_{n} = \frac{3n^{2}-n+2}{2}
Below is the implementation of above approach: 
 

C++




//CPP program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
#include<bits/stdc++.h>
using namespace std;
 
// calculate Nth term of given series
int Nth_Term(int n)
{
     
return (3 * pow(n, 2) - n + 2) / (2);
 
}
 
// Driver code
int main()
{
 
int N = 5;
cout<<Nth_Term(N)<<endl;
 
}


Java




//Java program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
import java.io.*;
 
class GFG {
 
// calculate Nth term of given series
static int Nth_Term(int n)
{
     
return (int)(3 * Math.pow(n, 2) - n + 2) / (2);
 
}
 
// Driver code
 
    public static void main (String[] args) {
    int N = 5;
    System.out.println(Nth_Term(N));
    }
}
// This code is contributed by anuj_67..


Python3




# Python program to find Nth term of the series
# 2 + 6 + 13 + 23 + 36 + ...
 
# calculate Nth term of given series
def Nth_Term(n):
    return (3 * pow(n, 2) - n + 2) // (2)
 
# Driver code
N = 5
print(Nth_Term(N))


C#




// C# program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
 
class GFG
{
 
// calculate Nth term of given series
static int Nth_Term(int n)
{
     
    return (int)(3 * System.Math.Pow(n, 2) -
                              n + 2) / (2);
}
 
// Driver code
static void Main ()
{
    int N = 5;
    System.Console.WriteLine(Nth_Term(N));
}
}
 
// This code is contributed by mits


PHP




<?php
// PHP program to find
// Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
 
// calculate Nth term of given series
function Nth_Term($n)
{
    return (3 * pow($n, 2) - $n + 2) / (2);
}
 
// Driver code
$N = 5;
echo (Nth_Term($N));
 
// This code is contributed
// by Sach_Code
?>


Javascript




<script>
 
// java script program to find
// Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
 
// calculate Nth term of given series
function Nth_Term(n)
{
    return (3 * Math.pow(n, 2) - n + 2) / (2);
}
 
// Driver code
let N = 5;
document.write (Nth_Term(N));
 
// This code is contributed
// by bobby
</script>


Output: 

36

 

Time complexity: O(1), since there is no loop or recursion.

Auxiliary Space: O(1), since no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button