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.
Input: A = 1, R = 0.5
Input: A = 1, R = -0.25
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:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.