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: 2Input: 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
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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to calculate the sum of// an infinite Geometric Progressionvoid 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 Codeint main(){ double A = 1, R = 0.5; findSumOfGP(A, R); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to calculate the sum of// an infinite Geometric Progressionstatic 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 Codepublic 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 Progressiondef 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 Codeif __name__ == '__main__': A, R = 1, 0.5 findSumOfGP(A, R)# This code is contributed by mohit kumar 29. |
C#
// C# program for the above approachusing 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 Progressionfunction 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 Codelet 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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



