Find the Nth term of the series 14, 28, 20, 40,….. using C++


In this problem, we are given an integer value N.Our task is to find the nth term of the series

14, 28, 20, 40, 32, 64, 56, 112….

Let’s take an example to understand the problem,

Input

N = 6

Output

64

Solution Approach

To find the Nth term of the series we need to find the general term of the series. For which we need to observe the series closely. I can see two different ways to solve the series.

Method 1

The series is a mixture of two different series at even and odd positions.

At odd positions − 14, 20, 32, 56, ….

T1 = 14
T3 = 20 = T1 + 6
T5 = 32 = T3 + 12
T7 = 56 = T5 + 24 = T1 + 6 + 12 + 24 = T1 + 6*(1 + 2 + 4)
TN = T1 + 6(20 + 21 + 22 +....+ 2((N/2) - 1 ) )

At even positions − 28, 40, 64, 112…

T2 = 28
T4 = 40 = T2 + 12
T6 = 64 = T4 + 24
T8 = 112 = T6 + 48 = T2 + 12 + 24 + 48 = T2 + 6*(2 + 4 + 8)
TN = T2 + 6(21 + 22 +....+ 2((N/2) - 1 ) )

The Nth term of the series is

$\mathrm{T_{N}\, =\, T_{s}\, +\, 6\left ( \sum 2^{\left ( \left ( N/2 \right )-1 \right )} \right )}$ ,where value are from s to N incremented by 2.

For even values s = 2,

For odd values s = 1.

Example

#include <iostream>
#include <math.h>
using namespace std;
long findNthAdd(int s, int i, int n){
   int sum = 0;
   for(; i <= n; i+= 2){
      sum += pow(2, (int)((i/2) - 1));
   }
   return 6*sum;
}
long findNthTermSeries(int n){
   int s, i;
   if(n % 2 == 0){
      s = 28;
      i = 4;
   }
   else{
      s = 14;
      i = 3;
   }
   return ( s + findNthAdd(s, i, n));
}
int main(){
   int n = 15;
   cout<<n<<"th term of the series is "<<findNthTermSeries(n);
   return 0;
}

Output

15th term of the series is 776

Another solution

One more way, using which the Nth term can be found is using the fact that the current term is either twice of the previous term or it 8 less from the previous term based on it’s even/odd.

If N is even TN = 2*T(N-1)
If N is odd TN = T(N-1) - 8

So, we need to loop from 2 to N, and find Ti by checking if its even or odd.

Example

Program to illustrate the working of our solution,

#include <iostream>
using namespace std;
bool isEven(int N){
   if(N % 2 == 0)
      return true;
   return false;
}
int findNthTermSeries(int n){
   int TermN = 14;
   for (int i = 2; i <= n; i++) {
      if (isEven(i))
         TermN *= 2;
      else
         TermN -= 8;
   }
   return TermN;
}
int main(){
   int n = 15;
   cout<<n<<"th term of the series is "<<findNthTermSeries(n);
   return 0;
}

Output

15th term of the series is 776

Updated on: 14-Feb-2022

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements