Skip to content

sum_of_geometric_progression #2168

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 10 commits into from
Jul 10, 2020
28 changes: 28 additions & 0 deletions maths/sum_of_geometric_progression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def sum_of_geometric_progression(
first_term: int, common_ratio: int, num_of_terms: int
) -> float:
""""
Return the sum of n terms in a geometric progression.
>>> sum_of_geometric_progression(1, 2, 10)
1023.0
>>> sum_of_geometric_progression(1, 10, 5)
11111.0
>>> sum_of_geometric_progression(0, 2, 10)
0.0
>>> sum_of_geometric_progression(1, 0, 10)
1.0
>>> sum_of_geometric_progression(1, 2, 0)
-0.0
>>> sum_of_geometric_progression(-1, 2, 10)
-1023.0
>>> sum_of_geometric_progression(1, -2, 10)
-341.0
>>> sum_of_geometric_progression(1, 2, -10)
-0.9990234375
"""
if common_ratio == 1:
# Formula for sum if common ratio is 1
return num_of_terms * first_term

# Formula for finding sum of n terms of a GeometricProgression
return (first_term / (1 - common_ratio)) * (1 - common_ratio ** num_of_terms)