Sum of Geometric Progression series (original) (raw)
Last Updated : 22 May, 2026
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
- [Naive Approach] Iterative Summation Method - O(N) Time and O(1) Space
- [Expected Approach] Direct Formula Method - O(log(n)) Time and O(1) Space
[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:
sumbecomes 0 + 2 = 2, thenabecomes 2 * 2 = 4 - i = 1:
sumbecomes 2 + 4 = 6, thenabecomes 4 * 2 = 8. - i = 2:
sumbecomes 6 + 8 = 14, thenabecomes 8 * 2 = 16. - i = 3:
sumbecomes 14 + 16 = 30, thenabecomes 16 * 2 = 32. - i = 4:
sumbecomes 30 + 32 = 62, thenabecomes 32 * 2 = 64. C++ `
#include <bits/stdc++.h> using namespace std;
// function to calculate sum of // geometric series int sumOfGP(int n, int a, int r) { int sum = 0;
// iterate n times to generate and
// add each term in the progression
for (int i = 0; i < n; i++)
{
sum = sum + a;
// multiplying with r to get next term
a = a * r;
}
return sum;}
// driver function
int main()
{
int a = 3;
int r = 2;
int n = 3;
cout << sumOfGP(n, a, r) << endl;
return 0;}
Java
import java.io.*;
class GFG{
// function to calculate sum of
// geometric series
static int sumOfGP(int n, int a, int r)
{
int sum = 0;
// iterate n times to generate and
// add each term in the progression
for (int i = 0; i < n; i++)
{
sum = sum + a;
// multiplying with r to get next term
a = a * r;
}
return sum;
}
// driver function
public static void main(String args[])
{
int a = 3;
int r = 2;
int n = 3 ;
System.out.printf(sumOfGP(n, a, r));
}}
Python
function to calculate sum of
geometric series
def sumOfGP(n, a, r) :
sum = 0
i = 0
# iterate n times to generate and
# add each term in the progression
while i < n :
sum = sum + a
# multiplying with r to get next term
a = a * r
i = i + 1
return sum#driver function
a = 3 r = 2 n = 3
print(sumOfGP(n, a, r)),
C#
using System;
class GFG {
// function to calculate
// sum of geometric series
static int sumOfGP(int n, int a, int r)
{
int sum = 0;
// iterate n times to generate and
// add each term in the progression
for (int i = 0; i < n; i++)
{
sum = sum + a;
// multiplying with r to get next term
a = a * r;
}
return sum;
}
// Driver Code
static public void Main ()
{
int a = 3;
int r = 2;
int n = 3;
Console.WriteLine((sumOfGP(n, a, r)));
}}
JavaScript
// function to calculate sum // of geometric series function sumOfGP(n, a, r) { let sum = 0;
// iterate n times to generate and
// add each term in the progression
for (let i = 0; i < n; i++) {
sum = sum + a;
// multiplying with r to get next term
a = a * r;
}
return sum;}
// Driver code
let a = 3;
let r = 2;
let n = 3;
console.log(sumOfGP(n, a, r))
`
[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 isr 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).
C++ `
#include <bits/stdc++.h> using namespace std;
// function to calculate sum of // geometric series int sumOfGP(int n, int a, int r) {
// if common ratio is 1, the series
// is simply a, a, a, ...(n times)
if (r == 1)
{
return a * n;
}
// applying standard geometric series
// formula : a * (r^n - 1) / (r - 1)
return (a * (pow(r, n) - 1)) / (r - 1);}
// driver code
int main()
{
int a = 3;
int r = 2;
int n = 3;
cout << sumOfGP(n, a, r);
return 0;}
Java
import java.math.*;
class GFG {
// function to calculate sum of
// geometric series
static int sumOfGP(int n, int a, int r)
{
// if common ratio is 1, the series
// is simply a, a, a, ...(n times)
if (r == 1) {
return a * n;
}
// applying standard geometric series
// formula : a * (r^n - 1) / (r - 1)
return (a * (int)(1 - (Math.pow(r, n)))) / (1 - r);
}
// driver code
public static void main(String args[])
{
int a = 3;
int r = 2;
int n = 3;
System.out.println((sumOfGP(n, a, r)));
}}
Python
function to calculate sum of
geometric series
def sumOfGP(n, a, r) :
# if common ratio is 1, the series
# is simply a, a, a, ...(n times)
if r == 1:
return a * n
# applying standard geometric series
# formula : a * (r^n - 1) / (r - 1)
return (a * (pow(r, n) - 1)) // (r - 1)driver code
a = 3
r = 2
n = 3
print sumOfGP(n, a, r)
C#
using System;
class GFG {
// function to calculate sum of
// geometric series
static int sumOfGP(int n, int a, int r)
{
// if common ratio is 1, the series
// is simply a, a, a, ...(n times)
if (r == 1) {
return a * n;
}
// applying standard geometric series
// formula : a * (r^n - 1) / (r - 1)
return (a * (1 - (Math.Pow(r, n)))) / (1 - r);
}
// Driver Code
public static void Main()
{
int a = 3;
int r = 2;
int n = 3;
Console.Write(sumOfGP(n, a, r));
}}
JavaScript
// function to calculate sum of // geometric series function sumOfGP(n, a, r) {
// if common ratio is 1, the series is
// simply a, a, a,...(n times)
if (r == 1) {
return a * n;
}
// applying standard geometric series
// formula : a * (r^n - 1) / (r - 1)
return (a * (Math.pow(r, n)) - 1) / (r - 1);}
// Driver code function main() { let a = 3; let r = 2; let n = 3;
console.log(sumOfGP(n, a, r));}
// Run the main function main();
`