![]() |
VOOZH | about |
Given n, a and r as the number of terms, first term and common ratio respectively of a Geometric Progression series. Find the sum of the series up to nth term.
The Geometric Progression series looks like :- a, ar, ar2, ar3, ar4, .....
Examples :
Input: n = 3, a = 3, r = 2
Output: 21
Explanation: Series up to 3rd term is 3 6 12, so sum will be 21.Input : n=3, a = 1, r = 2
Output : 7
Explanation: Series up to 3rd term is 1 2 4, so sum will be 7.
Table of Content
A simple solution is to one by one add terms to calculate the sum of geometric series.
Consider we have n = 5, a = 2 and r = 2.
Initializing sum = 0, and then iterating through all terms
sum becomes 0 + 2 = 2, then a becomes 2 * 2 = 4 sum becomes 2 + 4 = 6, then a becomes 4 * 2 = 8.sum becomes 6 + 8 = 14, then a becomes 8 * 2 = 16.sum becomes 14 + 16 = 30, then a becomes 16 * 2 = 32.sum becomes 30 + 32 = 62, then a becomes 32 * 2 = 64.21
An efficient solution to solve the sum of geometric series where first term is a and common ration is r is by the formula :- sum of series = a(1 - rn)/(1 - r). Where r = T2/T1 = T3/T2 = T4/T3 . . . Here T1, T2, T3, T4 . . . ,Tn are the first, second, third, . . . ,nth terms respectively.
Consider we have n = 5, a = 2 and r = 2.
Series is 2, 4, 8, 16, 32 which sum up to 62 = 2 * (25 - 1) / (2 - 1).
21