Skip to content

Created sum_of_harmonic_series.py #7504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions maths/sum_of_harmonic_series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def sum_of_harmonic_progression(
first_term: float, common_difference: float, number_of_terms: int
) -> float:
"""
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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are doctests.

>>> sum_of_harmonic_progression(1 / 2, 2, 2)
0.75
>>> sum_of_harmonic_progression(1 / 5, 5, 5)
0.45666666666666667
"""
arithmetic_progression = [1 / first_term]
first_term = 1 / first_term
for _ in range(number_of_terms - 1):
first_term += common_difference
arithmetic_progression.append(first_term)
harmonic_series = [1 / step for step in arithmetic_progression]
return sum(harmonic_series)


if __name__ == "__main__":
import doctest

doctest.testmod()
print(sum_of_harmonic_progression(1 / 2, 2, 2))