-
-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bbe97da
Created sum_of_harmonic_series.py
AkshitGulyan 4154aea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 69a9c24
Update maths/sum_of_harmonic_series.py
AkshitGulyan 4ef0e17
Update maths/sum_of_harmonic_series.py
AkshitGulyan 82dedcb
Update maths/sum_of_harmonic_series.py
AkshitGulyan e9cb590
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] eb63149
Update maths/sum_of_harmonic_series.py
AkshitGulyan ae928a5
Update maths/sum_of_harmonic_series.py
AkshitGulyan 2dfcc1e
Update maths/sum_of_harmonic_series.py
AkshitGulyan 24b2005
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1cea14e
Update sum_of_harmonic_series.py
AkshitGulyan 634f320
Add doctests
cclauss 646e166
Update sum_of_harmonic_series.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def sum_of_harmonic_progression( | ||
AkshitGulyan marked this conversation as resolved.
Show resolved
Hide resolved
AkshitGulyan marked this conversation as resolved.
Show resolved
Hide resolved
AkshitGulyan marked this conversation as resolved.
Show resolved
Hide resolved
AkshitGulyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add doctests here: https://docs.python.org/3/library/doctest.html There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
AkshitGulyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(sum_of_harmonic_progression(1 / 2, 2, 2)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.