Sum of an Infinite Geometric Progression ( GP ) - GeeksforGeeks

07 May, 2021

Given two integers A and R, representing the first term and the common ratio of a geometric sequence, the task is to find the sum of the infinite geometric series formed by the given first term and the common ratio.
Examples:
Input: A = 1, R = 0.5Output: 2
Input: A = 1, R = -0.25Output: 0.8
Approach: The given problem can be solved based on the following observations:
If absolute of value of R is greater than equal to 1, then the sum will be infinite.
Otherwise, the sum of the Geometric series with infinite terms can be calculated using the formula
Therefore, if the absolute value of R is greater than equal to 1, then print “Infinite”. Otherwise, print the value as the resultant sum.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
 

 
 










// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum of
// an infinite Geometric Progression
void findSumOfGP(double a, double r)
{
    // Case for Infinite Sum
    if (abs(r) >= 1) {
        cout << "Infinite";
        return;
    }
 
    // Store the sum of GP Series
    double sum = a / (1 - r);
 
    // Print the value of sum
    cout << sum;
}
 
// Driver Code
int main()
{
    double A = 1, R = 0.5;
    findSumOfGP(A, R);
 
    return 0;
}






 

 
 










// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate the sum of
// an infinite Geometric Progression
static void findSumOfGP(double a, double r)
{
     
    // Case for Infinite Sum
    if (Math.abs(r) >= 1)
    {
        System.out.print("Infinite");
        return;
    }
 
    // Store the sum of GP Series
    double sum = a / (1 - r);
 
    // Print the value of sum
    System.out.print(sum);
}
 
// Driver Code
public static void main(String[] args)
{
    double A = 1, R = 0.5;
    findSumOfGP(A, R);
}
}
 
// This code is contributed by 29AjayKumar






 

 
 










# Python3 program for the above approach
 
# Function to calculate the sum of
# an infinite Geometric Progression
def findSumOfGP(a, r):
   
    # Case for Infinite Sum
    if (abs(r) >= 1):
        print("Infinite")
        return
 
    # Store the sum of GP Series
    sum = a / (1 - r)
 
    # Print the value of sum
    print(int(sum))
 
# Driver Code
if __name__ == '__main__':
    A, R = 1, 0.5
    findSumOfGP(A, R)
 
# This code is contributed by mohit kumar 29.






 

 
 










// C# program for the above approach
using System;
class GFG
{
   
    // Function to calculate the sum of
    // an infinite Geometric Progression
    static void findSumOfGP(double a, double r)
    {
       
        // Case for Infinite Sum
        if (Math.Abs(r) >= 1) {
            Console.Write("Infinite");
            return;
        }
 
        // Store the sum of GP Series
        double sum = a / (1 - r);
 
        // Print the value of sum
        Console.Write(sum);
    }
 
    // Driver Code
    public static void Main()
    {
        double A = 1, R = 0.5;
        findSumOfGP(A, R);
    }
}
 
// This code is contributed by ukasp.






 

 
 










<script>
 
// JavaScript program for the above approach
 
// Function to calculate the sum of
// an infinite Geometric Progression
function findSumOfGP(a, r)
{
     
    // Case for Infinite Sum
    if (Math.abs(r) >= 1)
    {
        document.write("Infinite");
        return;
    }
 
    // Store the sum of GP Series
    let sum = a / (1 - r);
 
    // Print the value of sum
    document.write(sum);
}
 
// Driver Code
let A = 1, R = 0.5;
 
findSumOfGP(A, R);
 
// This code is contributed by sanjoy_62
     
</script>






2
 
Time Complexity: O(1)Auxiliary Space: O(1)
mohit kumar 29
ukasp
sanjoy_62
29AjayKumar

Geometric Progression


Sequence and Series


series


series-sum


Mathematical

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Count ways to reach the n'th stair
Check if a number is Palindrome
Singular Value Decomposition (SVD)
Fizz Buzz Implementation
Find first and last digits of a number