![]() |
VOOZH | about |
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: 80Input: 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-
80
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