Skip to content

Commit 6d6f7f2

Browse files
committed
Fix: Broadened type hints for geometric_progression to allow float inputs and added doctest
1 parent a71618f commit 6d6f7f2

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

maths/sum_of_geometric_progression.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from typing import Union
2+
# ... other imports ...
3+
14
def sum_of_geometric_progression(
2-
first_term: int, common_ratio: int, num_of_terms: int
5+
# FIX: Changed 'int' to 'Union[int, float]' to allow non-integer terms/ratios
6+
first_term: Union[int, float], common_ratio: Union[int, float], num_of_terms: int
37
) -> float:
4-
""" "
8+
"""
59
Return the sum of n terms in a geometric progression.
610
>>> sum_of_geometric_progression(1, 2, 10)
711
1023.0
@@ -19,10 +23,15 @@ def sum_of_geometric_progression(
1923
-341.0
2024
>>> sum_of_geometric_progression(1, 2, -10)
2125
-0.9990234375
26+
27+
# NEW DOCTEST to validate float inputs
28+
>>> sum_of_geometric_progression(0.5, 2, 3)
29+
3.5
2230
"""
2331
if common_ratio == 1:
2432
# Formula for sum if common ratio is 1
2533
return num_of_terms * first_term
2634

2735
# Formula for finding sum of n terms of a GeometricProgression
28-
return (first_term / (1 - common_ratio)) * (1 - common_ratio**num_of_terms)
36+
# Note: Python's standard float arithmetic handles this correctly for the fix.
37+
return (first_term / (1 - common_ratio)) * (1 - common_ratio**num_of_terms)

0 commit comments

Comments
 (0)