Program for sum of geometric series

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output :

1.99805

Time Complexity: O(n).

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



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.

This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, jit_t