Monday, December 15, 2025
HomeData Modelling & AISum of an Infinite Geometric Progression ( GP )

Sum of an Infinite Geometric Progression ( GP )

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.5
Output: 2

Input: A = 1, R = -0.25
Output: 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

Sum = \frac{A}{(1 - R)}

Therefore, if the absolute value of R is greater than equal to 1, then print “Infinite”. Otherwise, print the value \frac{A}{(1 - R)}           as the resultant sum.

Below is the implementation of the above approach:

C++




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




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




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




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


Javascript




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


Output: 

2

 

Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6955 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6883 POSTS0 COMMENTS