Finding n-th term of series 3, 13, 42, 108, 235... in C++



In this problem, we are given a number n. Our task is to find the n-th term of series 3, 13, 42, 108, 235...

Let's take an example to understand the problem,

Input : 5
Output : 235

Solution Approach

The series can be represented as the sum of cubes of first n natural numbers. The formula for that is (n*(n+1)/2)2. Also if we add 2* to it we will get the required series.

The formula for sum of the series is (n*(n+1)/2)2+2*n.

For n = 5 sum by the formula is

(5 * (5 + 1 ) / 2)) ^ 2 + 2*5

= (5 * 6 / 2) ^ 2 + 10

= (15) ^ 2 + 10

= 225 + 10

= 235

Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;
int findNthTerm(int N)
{
   return ((N * (N + 1) / 2)*(N * (N + 1) / 2) ) + 2 * N;
}
int main()
{
   int N = 5;
   cout<<"The Nth term fo the series n is "<<findNthTerm(N);
   return 0;
}

Output

The Nth term fo the series n is 235

Advertisements