Program for sum of geometric series

22 Jun, 2022
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++
Java
Python
C#
PHP
// A naive solution for calculating sum of// geometric series.#include<bits/stdc++.h>using namespace std; // function to calculate sum of// geometric seriesfloat 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 functionint 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;}
// 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
# A naive solution for calculating sum of# geometric series. # function to calculate sum of# geometric seriesdef 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 termr = (float)(1/2.0) # common ration = 10 # number of terms         print("%.5f" %sumOfGP(a, r, n)),     # This code is contributed by Nikita Tiwari
// 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.
<?php// A naive solution for calculating// sum of geometric series. // function to calculate sum // of geometric seriesfunction 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++
Java
Python
C#
PHP
// An Efficient solution to solve sum of// geometric series.#include<bits/stdc++.h>using namespace std; // function to calculate sum of// geometric seriesfloat sumOfGP(float a, float r, int n){    // calculating and storing sum    return (a * (1 - pow(r, n))) / (1 - r);} // driver codeint main(){    float a = 2; // first term    float r = 2; // common ratio    int n = 15; // number of terms     cout << sumOfGP(a, r, n);    return 0;}
// 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.
# An Efficient solution to solve sum of# geometric series. # function to calculate sum of# geometric seriesdef sumOfGP( a, r, n) :         # calculating and storing sum    return (a * (1 - pow(r, n))) / (1 - r)          # driver codea = 2 # first termr = 2 # common ration = 15 # number of terms print sumOfGP(a, r, n) # This code is contributed by Nikita Tiwari.
// 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.
<?php// An Efficient solution to solve// sum of geometric series. // function to calculate sum// of geometric seriesfunction 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
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
nitin mittal
jit_t
kumargaurav97520
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.