VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-sum-geometric-series/

⇱ Sum of Geometric Progression series - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of Geometric Progression series

Last Updated : 22 May, 2026

Given na 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.

[Naive Approach] Iterative Summation Method - O(N) Time and O(1) Space

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

  • i = 0: sum becomes 0 + 2 = 2, then a becomes 2 * 2 = 4
  • i = 1: sum becomes 2 + 4 = 6, then a becomes 4 * 2 = 8.
  • i = 2: sum becomes 6 + 8 = 14, then a becomes 8 * 2 = 16.
  • i = 3: sum becomes 14 + 16 = 30, then a becomes 16 * 2 = 32.
  • i = 4: sum becomes 30 + 32 = 62, then a becomes 32 * 2 = 64.

Output
21

[Expected Approach] Direct Formula Method - O(log(n)) Time and O(1) Space

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.

👁 sumofthenthtermofGP

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).


Output
21


Comment