859. Sum of GP Series

You are given a Geometric Progression (G.P.) defined by:

  • a: the first term
  • r: the common ratio
  • n: the number of terms

Write a function that returns the sum of the first n terms of this G.P.

Example 1:

Input: a = 1, r = 0.5, n = 3  

Output: 1.75  

Explanation: GP = [1, 0.5, 0.25] → Sum = 1 + 0.5 + 0.25 = 1.75

Example 2:

Input: a = 3, r = 5, n = 2  

Output: 18  

Explanation: GP = [3, 15] → Sum = 3 + 15 = 18

Now Your Turn!

Pick the correct output for the given input

Input: a = 2, r = 1, n = 4  

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • -1e6 ≤ a ≤ 1e6
  • -1e3 ≤ r ≤ 1e3
  • 1 ≤ n ≤ 10⁶
  • Return the result as a floating-point value up to 6 decimal places
0
class Solution {
public:
double sumOfGP(double a, double r, int n) {
// Your code goes here
}
};
 
Test Case

Input:

A
N
R