MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / sum_of_harmonic_progression

Function sum_of_harmonic_progression

maths/sum_of_harmonic_series.py:1–22  ·  view source on GitHub ↗

https://en.wikipedia.org/wiki/Harmonic_progression_(mathematics) Find the sum of n terms in an harmonic progression. The calculation starts with the first_term and loops adding the common difference of Arithmetic Progression by which the given Harmonic Progression is linked.

(
    first_term: float, common_difference: float, number_of_terms: int
)

Source from the content-addressed store, hash-verified

1def sum_of_harmonic_progression(
2 first_term: float, common_difference: float, number_of_terms: int
3) -> float:
4 """
5 https://en.wikipedia.org/wiki/Harmonic_progression_(mathematics)
6
7 Find the sum of n terms in an harmonic progression. The calculation starts with the
8 first_term and loops adding the common difference of Arithmetic Progression by which
9 the given Harmonic Progression is linked.
10
11 >>> sum_of_harmonic_progression(1 / 2, 2, 2)
12 0.75
13 >>> sum_of_harmonic_progression(1 / 5, 5, 5)
14 0.45666666666666667
15 """
16 arithmetic_progression = [1 / first_term]
17 first_term = 1 / first_term
18 for _ in range(number_of_terms - 1):
19 first_term += common_difference
20 arithmetic_progression.append(first_term)
21 harmonic_series = [1 / step for step in arithmetic_progression]
22 return sum(harmonic_series)
23
24
25if __name__ == "__main__":

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected