Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ... - GeeksforGeeks

12 Apr, 2021
Given an integer . The task is to write a program to find the N-th term of the given series:  
1 + 2 + 6 + 15 + 31 + 56 + ... 
Examples:  
Input : N = 8
Output : 141

Input : N = 20
Output : 2471
Approach: The given series is:  
1, 2, 6, 15, 31, 56, 92, 141, ...
First consecutive difference:  
1 4 9 16 25 36 49 .......
Second consecutive difference:  
3 5 7 9 11 13......
As the second consecutive difference is in AP, the nth term (tn) of the series is of the form,A(n – 1)(n – 2)(n – 3) + B(n – 1)(n – 2) + C(n – 1) + DSo, tn = A(n – 1)(n – 2)(n – 3) + B(n – 1)(n – 2) + C(n – 1) + DNow, t1 = D = 1t2 = C (2 – 1) + D = 2 t3 = 2B + 2C + D = 6t4 = CA + 6B + 3C + D = 15On solving the above four equations we get => A = 1/3, B = 3/2, C = 1, D = 1. On substituting these values tn and after simplifying we get,Below is the implementation of above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find Nth// term of the series:// 1 + 2 + 6 + 15 + 31 + 56 + ...#include<iostream>#include<math.h>using namespace std; // calculate Nth term of given seriesint Nth_Term(int n){    return (2 * pow(n, 3) - 3 *                pow(n, 2) + n + 6) / 6;} // Driver codeint main(){    int N = 8;    cout << Nth_Term(N);}
// Java program to find Nth// term of the series:// 1 + 2 + 6 + 15 + 31 + 56 + ...import java.util.*;import java.lang.*; class GFG{// calculate Nth term of given seriesstatic double Nth_Term(int n){    return (2 * Math.pow(n, 3) - 3 *                Math.pow(n, 2) + n + 6) / 6;} // Driver codestatic public void main (String args[]){    int N = 8;    System.out.println(Nth_Term(N));}} // This code is contributed// by Akanksha Rai
# Python program to find Nth term of the series:# 1 + 2 + 6 + 15 + 31 + 56 + ... # calculate Nth term of given seriesdef Nth_Term(n):    return (2 * pow(n, 3) - 3 * pow(n, 2) + n + 6) // 6 # Driver codeN = 8print(Nth_Term(N))
// C# program to find Nth// term of the series:// 1 + 2 + 6 + 15 + 31 + 56 + ...using System; class GFG{// calculate Nth term of given seriesstatic double Nth_Term(int n){    return (2 * Math.Pow(n, 3) - 3 *                Math.Pow(n, 2) + n + 6) / 6;} // Driver codestatic public void Main (){    int N = 8;    Console.WriteLine(Nth_Term(N));}} // This code is contributed// by Sach_Code
<?php// PHP program to find Nth// term of the series:// 1 + 2 + 6 + 15 + 31 + 56 + .. // calculate Nth term of given seriesfunction Nth_Term($n){    return (2 * pow($n, 3) - 3 *                pow($n, 2) + $n + 6) / 6;} // Driver code$N = 8; echo Nth_Term($N); // This code is contributed by// Shashank_Sharma?>
<script>// js program to find Nth// term of the series:// 1 + 2 + 6 + 15 + 31 + 56 + .. // calculate Nth term of given seriesfunction Nth_Term(n){    return (2 * Math.pow(n, 3) - 3 *                Math.pow(n, 2) + n + 6) / 6;} // Driver codelet N = 8; document.write(Nth_Term(N)); // This code is contributed// by pulamolu mohan pavan cse</script>
141
 
SoumikMondal
Shashank_Sharma
Sach_Code
Akanksha_Rai
pulamolusaimohan
arithmetic progression
GetNth
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Sieve of Eratosthenes
Program for Decimal to Binary Conversion
Euclidean algorithms (Basic and Extended)