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] [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 d5265153e..a7da29b79 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()