File tree 1 file changed +4
-2
lines changed
1 file changed +4
-2
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
This script demonstrates the implementation of the sum of squares of the first n natural numbers.
3
3
4
- The function takes an integer n as input and returns the sum of squares
4
+ The function takes an integer n as input and returns the sum of squares
5
5
from 1 to n using the formula n(n + 1)(2n + 1) / 6. This formula computes the sum efficiently
6
6
without the need for iteration.
7
7
8
8
https://www.cuemath.com/algebra/sum-of-squares/
9
9
"""
10
10
11
+
11
12
def sum_of_squares (n : int ) -> int :
12
13
"""
13
14
Implements the sum of squares formula for the first n natural numbers.
@@ -25,9 +26,10 @@ def sum_of_squares(n: int) -> int:
25
26
>>> sum_of_squares(10)
26
27
385
27
28
"""
28
- return n * (n + 1 ) * (2 * n + 1 ) // 6
29
+ return n * (n + 1 ) * (2 * n + 1 ) // 6
29
30
30
31
31
32
if __name__ == "__main__" :
32
33
import doctest
34
+
33
35
doctest .testmod ()
You can’t perform that action at this time.
0 commit comments