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
Below is the implementation of the above approach:
// 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> |
Output:
2
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.