0 out of 464 challenges solved
Write a function `geometric_sum(n)` that calculates the geometric sum of `n-1` using recursion. The geometric sum is defined as: \[ S(n) = \sum_{i=0}^{n} \frac{1}{2^i} \] For example: - `geometric_sum(0)` should return `1.0` (since \( \frac{1}{2^0} = 1 \)). - `geometric_sum(1)` should return `1.5` (since \( \frac{1}{2^0} + \frac{1}{2^1} = 1.5 \)). - `geometric_sum(2)` should return `1.75` (since \( \frac{1}{2^0} + \frac{1}{2^1} + \frac{1}{2^2} = 1.75 \)). #### Example Usage ```python [main.nopy] print(geometric_sum(3)) # Output: 1.875 print(geometric_sum(5)) # Output: 1.96875 ``` #### Constraints - The function should use recursion. - Assume `n` is a non-negative integer.
def geometric_sum(n): """ Calculate the geometric sum of n-1 using recursion. Args: n (int): The input non-negative integer. Returns: float: The geometric sum. """ # Base case: when n is 0 # Recursive case: calculate the sum for n-1 pass