Skip to content

Commit ed12703

Browse files
AkshitGulyanpre-commit-ci[bot]ChrisO345cclauss
authored
Created sum_of_harmonic_series.py (TheAlgorithms#7504)
* Created sum_of_harmonic_series.py Here in this code the formula for Harmonic sum is not used, Sum of the series is calculated by creating a list of the elements in the given Harmonic series and adding all the elements of that list ! * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/sum_of_harmonic_series.py Co-authored-by: Chris O <[email protected]> * Update maths/sum_of_harmonic_series.py Co-authored-by: Chris O <[email protected]> * Update maths/sum_of_harmonic_series.py Co-authored-by: Chris O <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/sum_of_harmonic_series.py Co-authored-by: Christian Clauss <[email protected]> * Update maths/sum_of_harmonic_series.py Co-authored-by: Christian Clauss <[email protected]> * Update maths/sum_of_harmonic_series.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update sum_of_harmonic_series.py * Add doctests * Update sum_of_harmonic_series.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Chris O <[email protected]> Co-authored-by: Christian Clauss <[email protected]>
1 parent a5dd07c commit ed12703

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

maths/sum_of_harmonic_series.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def 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+
25+
if __name__ == "__main__":
26+
import doctest
27+
28+
doctest.testmod()
29+
print(sum_of_harmonic_progression(1 / 2, 2, 2))

0 commit comments

Comments
 (0)