Open In App

Program for sum of geometric series

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and common ratio is denoted by r. The series looks like this :- a, ar, ar2, ar3, ar4, . . .. The task is to find the sum of such a series. Examples :

Input : a = 1
r = 0.5
n = 3
Output : 1.75
We get sum as 1 + 1 * 0.5 + 1 * 0.5 * 0.5 = 1 + 0.5 + 0.25 = 1.75

Input : a = 2
r = 2
n = 15
Output : 65534

[Naive Approach] Iterative Summation Method

A Simple solution is to one by one add terms to calculate the sum of geometric series. 

C++
// A naive solution for calculating sum of
// geometric series.
#include<bits/stdc++.h>
using namespace std;

// function to calculate sum of 
// geometric series
float sumOfGP(float a, float r, int n)
{
    float sum = 0; 
    for (int i = 0; i < n; i++)
    {
        sum = sum + a;
        a = a * r;
    }
    return sum;
}

// driver function
int main()
{
    int a = 1; // first term
    float r = 0.5; // common ratio
    int n = 3; // number of terms

    cout << sumOfGP(a, r, n) << endl;
    return 0;
}
C
// A naive solution for calculating sum of
// geometric series.
#include <stdio.h>

// function to calculate sum of 
// geometric series
float sumOfGP(float a, float r, int n)
{
    float sum = 0; 
    for (int i = 0; i < n; i++)
    {
        sum = sum + a;
        a = a * r;
    }
    return sum;
}

// driver function
int main()
{
    int a = 1; // first term
    float r = 0.5; // common ratio
    int n = 10; // number of terms

    printf("%f\n", sumOfGP(a, r, n));
    return 0;
}
Java
// A naive solution for calculating sum of
// geometric series.
import java.io.*;

class GFG{
    
    // function to calculate sum of 
    // geometric series
    static float sumOfGP(float a, float r, int n)
    {
        float sum = 0; 
        for (int i = 0; i < n; i++)
        {
            sum = sum + a;
            a = a * r;
        }
        return sum;
    }

    // driver function
    public static void main(String args[])
    {
        int a = 1; // first term
        float r = (float)(1/2.0) ;// common ratio
        int n = 10 ; // number of terms
        
        System.out.printf("%.5f",(sumOfGP(a, r, n)));
    }
    
}
Python
# A naive solution for calculating sum of
# geometric series.

# function to calculate sum of 
# geometric series
def sumOfGP(a, r, n) :
    
    sum = 0
    i = 0
    while i < n :
        sum = sum + a
        a = a * r
        i = i + 1
    
    return sum
    
#driver function

a = 1 # first term
r = (float)(1/2.0) # common ratio
n = 10 # number of terms
        
print("%.5f" %sumOfGP(a, r, n)),
    
C#
// A naive solution for calculating
// sum of geometric series.
using System;

class GFG {
    
    // function to calculate 
    // sum of geometric series
    static float sumOfGP(float a, 
                         float r, 
                         int n)
    {
        float sum = 0; 
        for (int i = 0; i < n; i++)
        {
            sum = sum + a;
            a = a * r;
        }
        return sum;
    }

    // Driver Code
    static public void Main ()
    {
        
        // first term
        int a = 1; 
        
        // common ratio
        float r = (float)(1/2.0) ;
        
        // number of terms
        int n = 10 ; 
        
        Console.WriteLine((sumOfGP(a, r, n)));
    }
    
}
JavaScript
    // function to calculate sum of 
    // geometric series
    function sumOfGP(a, r, n) {
        let sum = 0;
        for (let i = 0; i < n; i++) {
            sum = sum + a;
            a = a * r;
        }
        return sum;
    }

    // Driver code
    let a = 1; // first term
    let r = 0.5; // common ratio
    let n = 10; // number of terms

    console.log(sumOfGP(a, r, n))
PHP
<?php
// A naive solution for calculating 
// sum of geometric series.

// function to calculate sum  
// of geometric series
function sumOfGP($a, $r, $n)
{
    $sum = 0; 
    for ($i = 0; $i < $n; $i++)
    {
        $sum = $sum + $a;
        $a = $a * $r;
    }
    return $sum;
}

// Driver Code

// first term
$a = 1; 

// common ratio
$r = 0.5; 

// number of terms
$n = 10; 

echo(sumOfGP($a, $r, $n));

?>

Output :

1.99805

Time Complexity : O(n)
Auxiliary Space : O(1)

[Expected Approach] Direct Formula Method

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

For example - The series is 2, 4, 8, 16, 32, 64, . . . upto 15 elements. In the above series, find the sum of first 15 elements where first term a = 2 and common ration r = 4/2 = 2 or = 8/4 = 2 Then, sum = 2 * (1 - 215) / (1 - 2). sum = 65534 

C++
// An Efficient solution to solve sum of 
// geometric series.
#include<bits/stdc++.h>
using namespace std;

// function to calculate sum of 
// geometric series
float sumOfGP(float a, float r, int n)
{
    // calculating and storing sum
    return (a * (1 - pow(r, n))) / (1 - r);
}

// driver code
int main()
{
    float a = 2; // first term
    float r = 2; // common ratio
    int n = 15; // number of terms

    cout << sumOfGP(a, r, n);
    return 0;
}
C
// An Efficient solution to solve sum of 
// geometric series.
#include <stdio.h>
#include <math.h>

// function to calculate sum of 
// geometric series
float sumOfGP(float a, float r, int n)
{
    // calculating and storing sum
    return (a * (1 - pow(r, n))) / (1 - r);
}

// driver code
int main()
{
    float a = 2; // first term
    float r = 2; // common ratio
    int n = 15; // number of terms

    printf("%f", sumOfGP(a, r, n));
    return 0;
}
Java
// An Efficient solution to solve sum of 
// geometric series.
import java.math.*;

class GFG{
    
    // function to calculate sum of 
    // geometric series
    static float sumOfGP(float a, float r, int n)
    {
        // calculating and storing sum
        return (a * (1 - (int)(Math.pow(r, n)))) / 
                                            (1 - r);
    }

    // driver code
    public static void main(String args[])
    {
        float a = 2; // first term
        float r = 2; // common ratio
        int n = 15; // number of terms

        System.out.println((int)(sumOfGP(a, r, n)));
    }
}
Python
# An Efficient solution to solve sum of 
# geometric series.

# function to calculate sum of 
# geometric series
def sumOfGP( a, r, n) :
    
    # calculating and storing sum
    return (a * (1 - pow(r, n))) / (1 - r)
    
    
# driver code
a = 2 # first term
r = 2 # common ratio
n = 15 # number of terms

print sumOfGP(a, r, n)
C#
// C# program to An Efficient solution
// to solve sum of geometric series.
using System;

class GFG {
    
    // function to calculate sum of 
    // geometric series
    static float sumOfGP(float a, float r, int n)
    {
        // calculating and storing sum
        return (a * (1 - (int)(Math.Pow(r, n)))) / 
                                       (1 - r);
    }

    // Driver Code
    public static void Main()
    {
        float a = 2; // first term
        float r = 2; // common ratio
        int n = 15; // number of terms

        Console.Write((int)(sumOfGP(a, r, n)));
    }
}
JavaScript
// Function to calculate sum of geometric series
function sumOfGP(a, r, n) {
    // calculating and storing sum
    return (a * (1 - Math.pow(r, n))) / (1 - r);
}

// Driver code
function main() {
    let a = 2; // first term
    let r = 2; // common ratio
    let n = 15; // number of terms

    console.log(sumOfGP(a, r, n));
}

// Run the main function
main();
PHP
<?php
// An Efficient solution to solve 
// sum of geometric series.

// function to calculate sum 
// of geometric series
function sumOfGP($a, $r, $n)
{
    // calculating and storing sum
    return ($a * (1 - pow($r, $n))) / 
                         (1 - $r);
}

// Driver Code

// first term
$a = 2; 

// common ratio
$r = 2; 

// number of terms
$n = 15; 

echo(sumOfGP($a, $r, $n));

?>

Output :

65534

Time Complexity: O(Log n) (Depends on implementation of pow() function in C/C++. In general, we can compute integer powers in log(n) time.
Auxiliary Space : O(1)


Practice Tags :

Similar Reads