0 out of 464 challenges solved
Write a Python function `harmonic_sum(n)` that calculates the harmonic sum of the first `n` terms. The harmonic sum is defined as:
\[
H(n) = \sum_{k=1}^{n} \frac{1}{k} = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}
\]
#### Example Usage
```python [main.nopy]
print(harmonic_sum(7)) # Output: 2.5928571428571425
print(harmonic_sum(4)) # Output: 2.083333333333333
print(harmonic_sum(19)) # Output: 3.547739657143682
```
#### Constraints
- The input `n` will be a positive integer.
- The function should use recursion to calculate the harmonic sum.def harmonic_sum(n):
"""
Calculate the harmonic sum of the first n terms.
Args:
n (int): The number of terms to include in the harmonic sum.
Returns:
float: The harmonic sum of the first n terms.
"""
# Base case: if n is 1, return 1
# Recursive case: add 1/n to the harmonic sum of (n-1)
pass