Open In App
Related Articles

Program for sum of geometric series

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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 = 10
Output : 1.99805

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

A Simple solution 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 = 10; // number of terms
 
    cout << sumOfGP(a, r, n) << endl;
    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)));
    }
     
}
 
//This code is contributed by Nikita Tiwari

                    

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)),
     
# This code is contributed by Nikita Tiwari

                    

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)));
    }
     
}
 
// This code is contributed by Ajit.

                    

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))
 
// This code is contributed by vikkycirus

                    

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));
 
// This code is contributed by Ajit.
?>

                    

Output :

1.99805

Time Complexity: O(n)

Auxiliary Space: O(1)

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 . . . and T1, T2, T3, T4 . . . ,Tn are the first, second, third, . . . ,nth terms respectively. 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;
}

                    

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)));
    }
}
 
//This code is contributed by Nikita Tiwari.

                    

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)
 
# This code is contributed by Nikita Tiwari.

                    

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)));
    }
}
 
// This code is contributed by Nitin Mittal.

                    

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));
 
// This code is contributed by Ajit.
?>

                    

Output :

65534

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

Space Complexity: O(1) since using constant variables



Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads