From 32634eff5abab3fd462cd77d3c89c5d6569b6128 Mon Sep 17 00:00:00 2001 From: Harsh Gupta Date: Sat, 26 Oct 2024 00:52:18 +0530 Subject: [PATCH 1/2] Add doctests for sum_of_arithmetic_series function --- maths/sum_of_arithmetic_series.py | 57 ++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/maths/sum_of_arithmetic_series.py b/maths/sum_of_arithmetic_series.py index 3e381b8c2..3b53b1181 100644 --- a/maths/sum_of_arithmetic_series.py +++ b/maths/sum_of_arithmetic_series.py @@ -1,23 +1,40 @@ -# DarkCoder -def sum_of_series(first_term: int, common_diff: int, num_of_terms: int) -> float: +#Reeka +def sum_of_ap_series(a: int, d: int, n: int) -> int: """ - Find the sum of n terms in an arithmetic progression. + Calculates the sum of the first 'n' terms of an arithmetic progression (AP) + series with the first term 'a' and common difference 'd'. + + Parameters: + a (int): The first term of the AP. + d (int): The common difference between terms. + n (int): The number of terms to sum. - >>> sum_of_series(1, 1, 10) - 55.0 - >>> sum_of_series(1, 10, 100) - 49600.0 + Returns: + int: The sum of the first 'n' terms of the AP. + + Examples: + >>> sum_of_ap_series(1, 1, 5) # Sum of first 5 natural numbers + 15 + >>> sum_of_ap_series(2, 3, 4) # Sum of 2, 5, 8, 11 + 26 + >>> sum_of_ap_series(5, 0, 3) # Sum of 5, 5, 5 + 15 + >>> sum_of_ap_series(1, 2, 1) # Single term AP series + 1 + >>> sum_of_ap_series(1, -1, 5) # Decreasing AP series + -5 + >>> sum_of_ap_series(1, 1, -5) # Negative 'n' should raise an error + Traceback (most recent call last): + ... + ValueError: Number of terms 'n' must be a positive integer + >>> sum_of_ap_series(1, 1, 0) # Zero terms should also raise an error + Traceback (most recent call last): + ... + ValueError: Number of terms 'n' must be a positive integer """ - total = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) - # formula for sum of series - return total - - -def main(): - print(sum_of_series(1, 1, 10)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() + if n <= 0: + raise ValueError("Number of terms 'n' must be a positive integer") + + # Formula for the sum of an AP series: S_n = n/2 * (2a + (n-1) * d) + return n * (2 * a + (n - 1) * d) // 2 +#Reeka From 7800f1e6458521ccac6f547f56445ec156a4511b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:37:29 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sum_of_arithmetic_series.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/maths/sum_of_arithmetic_series.py b/maths/sum_of_arithmetic_series.py index 3b53b1181..b320fe477 100644 --- a/maths/sum_of_arithmetic_series.py +++ b/maths/sum_of_arithmetic_series.py @@ -1,9 +1,9 @@ -#Reeka +# Reeka def sum_of_ap_series(a: int, d: int, n: int) -> int: """ Calculates the sum of the first 'n' terms of an arithmetic progression (AP) series with the first term 'a' and common difference 'd'. - + Parameters: a (int): The first term of the AP. d (int): The common difference between terms. @@ -34,7 +34,9 @@ def sum_of_ap_series(a: int, d: int, n: int) -> int: """ if n <= 0: raise ValueError("Number of terms 'n' must be a positive integer") - + # Formula for the sum of an AP series: S_n = n/2 * (2a + (n-1) * d) return n * (2 * a + (n - 1) * d) // 2 -#Reeka + + +# Reeka