Find Nth term of the series 5, 10, 20, 40…
Last Updated :
25 Apr, 2023
Given a positive integer N, the task is to find the Nth term of the series
5, 10, 20, 40….till N terms
Examples:
Input: N = 5
Output: 80
Input: N = 3
Output: 20
Approach:
1st term = 5 * (2 ^ (1 – 1)) = 5
2nd term = 5 * (2 ^ (2 – 1)) = 10
3rd term = 5 * (2 ^ (3 – 1)) = 20
4th term = 5 * (2 ^ (4 – 1)) = 40
.
.
Nth term = 5 * (2 ^ (N – 1))
The Nth term of the given series can be generalized as-
TN = (a * (r ^ (N – 1))
The following steps can be followed to derive the formula-
The series 5, 10, 20, 40….till N terms
is in G.P. with
first term a = 5
common ratio r = 2 because each term is double the one before it.
The Nth term of a G.P. is
TN = (a * (r ^ (N – 1))
Illustration:
Input: N = 5
Output: 80
Explanation:
TN = (a * (r ^ (N – 1))
= (5 * (2 ^ (5 – 1))
= (5 * 16)
= 80
Below is the implementation of the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
int nTerm( int a, int r, int n)
{
return a * pow (r, n - 1);
}
int main()
{
int N = 5;
int a = 5;
int r = 2;
cout << nTerm(a, r, N);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int nTerm( int a, int r, int n)
{
return a * pow (r, n - 1);
}
int main()
{
int N = 5;
int a = 5;
int r = 2;
printf ( "%d" , nTerm(a, r, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 5 ;
int a = 5 ;
int r = 2 ;
System.out.println(nTerm(a, r, N));
}
public static int nTerm( int a, int r, int n)
{
return a * (( int )Math.pow(r, n - 1 ));
}
}
|
Python3
def nTerm(a, r, n):
return a * pow (r, n - 1 )
if __name__ = = "__main__" :
N = 5
a = 5
r = 2
print (nTerm(a, r, N))
|
C#
using System;
public class GFG
{
public static int nTerm( int a, int r, int n)
{
return a * (( int )Math.Pow(r, n - 1));
}
static public void Main()
{
int N = 5;
int a = 5;
int r = 2;
Console.Write(nTerm(a, r, N));
}
}
|
Javascript
<script>
function nTerm(a, r, n) {
return a * Math.pow(r, n - 1);
}
let N = 5;
let a = 5;
let r = 2;
document.write(nTerm(a, r, N));
</script>
|
Time complexity: O(logrn) because it is using inbuilt pow function
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...