From 37c0953597d3e6f3e077a7d12bb7d0e3d1c2c7ec Mon Sep 17 00:00:00 2001 From: Byte Bender <136265142+BYT-Bender@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:13:36 +0530 Subject: [PATCH 1/5] Create sum_of_squares.py --- maths/sum_of_squares.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maths/sum_of_squares.py diff --git a/maths/sum_of_squares.py b/maths/sum_of_squares.py new file mode 100644 index 000000000000..1af5d0b8bd75 --- /dev/null +++ b/maths/sum_of_squares.py @@ -0,0 +1,31 @@ +""" +This script demonstrates the implementation of the sum of squares of the first n natural numbers. + +The function takes an integer n as input and returns the sum of squares +from 1 to n using the formula n(n + 1)(2n + 1) / 6. This formula computes the sum efficiently +without the need for iteration. +""" + +def sum_of_squares(n: int) -> int: + """ + Implements the sum of squares formula for the first n natural numbers. + + Parameters: + n (int): A positive integer representing the limit of the series + + Returns: + sum_squares (int): The sum of squares of the first n natural numbers. + + Examples: + >>> sum_of_squares(5) + 55 + + >>> sum_of_squares(10) + 385 + """ + return n * (n + 1) * (2*n + 1) // 6 + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 78849a74378f858a0e30db3326402236fc71070e Mon Sep 17 00:00:00 2001 From: Byte Bender <136265142+BYT-Bender@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:28:50 +0530 Subject: [PATCH 2/5] Update sum_of_squares.py --- maths/sum_of_squares.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/sum_of_squares.py b/maths/sum_of_squares.py index 1af5d0b8bd75..d5265153eda9 100644 --- a/maths/sum_of_squares.py +++ b/maths/sum_of_squares.py @@ -4,6 +4,8 @@ The function takes an integer n as input and returns the sum of squares from 1 to n using the formula n(n + 1)(2n + 1) / 6. This formula computes the sum efficiently without the need for iteration. + +https://www.cuemath.com/algebra/sum-of-squares/ """ def sum_of_squares(n: int) -> int: From ee5dbdda82542b119d4066dfff8b36f28e104f01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 05:01:19 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sum_of_squares.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/sum_of_squares.py b/maths/sum_of_squares.py index d5265153eda9..a7da29b79305 100644 --- a/maths/sum_of_squares.py +++ b/maths/sum_of_squares.py @@ -1,13 +1,14 @@ """ This script demonstrates the implementation of the sum of squares of the first n natural numbers. -The function takes an integer n as input and returns the sum of squares +The function takes an integer n as input and returns the sum of squares from 1 to n using the formula n(n + 1)(2n + 1) / 6. This formula computes the sum efficiently without the need for iteration. https://www.cuemath.com/algebra/sum-of-squares/ """ + def sum_of_squares(n: int) -> int: """ Implements the sum of squares formula for the first n natural numbers. @@ -25,9 +26,10 @@ def sum_of_squares(n: int) -> int: >>> sum_of_squares(10) 385 """ - return n * (n + 1) * (2*n + 1) // 6 + return n * (n + 1) * (2 * n + 1) // 6 if __name__ == "__main__": import doctest + doctest.testmod() From 7e51932ece2c0de2032b2834df62954e3c64f622 Mon Sep 17 00:00:00 2001 From: Byte Bender <136265142+BYT-Bender@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:39:51 +0530 Subject: [PATCH 4/5] Update sum_of_squares.py --- maths/sum_of_squares.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/maths/sum_of_squares.py b/maths/sum_of_squares.py index a7da29b79305..059befe939fa 100644 --- a/maths/sum_of_squares.py +++ b/maths/sum_of_squares.py @@ -1,8 +1,11 @@ """ -This script demonstrates the implementation of the sum of squares of the first n natural numbers. +This script demonstrates the implementation of the +sum of squares of the first n natural numbers. The function takes an integer n as input and returns the sum of squares -from 1 to n using the formula n(n + 1)(2n + 1) / 6. This formula computes the sum efficiently +from 1 to n using the formula n(n + 1)(2n + 1) / 6. + +This formula computes the sum efficiently without the need for iteration. https://www.cuemath.com/algebra/sum-of-squares/ @@ -11,7 +14,7 @@ def sum_of_squares(n: int) -> int: """ - Implements the sum of squares formula for the first n natural numbers. + Implements the sum of squares formulafor the first n natural numbers. Parameters: n (int): A positive integer representing the limit of the series From 2dfeac1c4239f0c3bf2279fb5b605595754a19d2 Mon Sep 17 00:00:00 2001 From: Byte Bender <136265142+BYT-Bender@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:50:42 +0530 Subject: [PATCH 5/5] Update sum_of_squares.py --- maths/sum_of_squares.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/sum_of_squares.py b/maths/sum_of_squares.py index 059befe939fa..8fec79ae30ca 100644 --- a/maths/sum_of_squares.py +++ b/maths/sum_of_squares.py @@ -12,12 +12,12 @@ """ -def sum_of_squares(n: int) -> int: +def sum_of_squares(num_of_terms: int) -> int: """ Implements the sum of squares formulafor the first n natural numbers. Parameters: - n (int): A positive integer representing the limit of the series + num_of_terms (int): A positive integer representing the limit of the series Returns: sum_squares (int): The sum of squares of the first n natural numbers. @@ -29,7 +29,7 @@ def sum_of_squares(n: int) -> int: >>> sum_of_squares(10) 385 """ - return n * (n + 1) * (2 * n + 1) // 6 + return num_of_terms * (num_of_terms + 1) * (2 * num_of_terms + 1) // 6 if __name__ == "__main__":