File tree Expand file tree Collapse file tree 4 files changed +24
-14
lines changed Expand file tree Collapse file tree 4 files changed +24
-14
lines changed Original file line number Diff line number Diff line change 15
15
"""
16
16
17
17
18
- def solution (n ) :
18
+ def solution (n : int ) -> int :
19
19
"""Returns the difference between the sum of the squares of the first n
20
20
natural numbers and the square of the sum.
21
21
@@ -28,14 +28,16 @@ def solution(n):
28
28
>>> solution(50)
29
29
1582700
30
30
"""
31
- suma = 0
32
- sumb = 0
31
+ sum_of_squares = 0
32
+ sum_of_ints = 0
33
33
for i in range (1 , n + 1 ):
34
- suma += i ** 2
35
- sumb += i
36
- sum = sumb ** 2 - suma
37
- return sum
34
+ sum_of_squares += i ** 2
35
+ sum_of_ints += i
36
+ return sum_of_ints ** 2 - sum_of_squares
38
37
39
38
40
39
if __name__ == "__main__" :
40
+ import doctest
41
+
42
+ doctest .testmod ()
41
43
print (solution (int (input ().strip ())))
Original file line number Diff line number Diff line change 15
15
"""
16
16
17
17
18
- def solution (n ) :
18
+ def solution (n : int ) -> int :
19
19
"""Returns the difference between the sum of the squares of the first n
20
20
natural numbers and the square of the sum.
21
21
@@ -28,11 +28,13 @@ def solution(n):
28
28
>>> solution(50)
29
29
1582700
30
30
"""
31
- suma = n * (n + 1 ) / 2
32
- suma **= 2
33
- sumb = n * (n + 1 ) * (2 * n + 1 ) / 6
34
- return int (suma - sumb )
31
+ sum_cubes = (n * (n + 1 ) // 2 ) ** 2
32
+ sum_squares = n * (n + 1 ) * (2 * n + 1 ) // 6
33
+ return sum_cubes - sum_squares
35
34
36
35
37
36
if __name__ == "__main__" :
37
+ import doctest
38
+
39
+ doctest .testmod ()
38
40
print (solution (int (input ().strip ())))
Original file line number Diff line number Diff line change 16
16
import math
17
17
18
18
19
- def solution (n ) :
19
+ def solution (n : int ) -> int :
20
20
"""Returns the difference between the sum of the squares of the first n
21
21
natural numbers and the square of the sum.
22
22
@@ -35,4 +35,7 @@ def solution(n):
35
35
36
36
37
37
if __name__ == "__main__" :
38
+ import doctest
39
+
40
+ doctest .testmod ()
38
41
print (solution (int (input ().strip ())))
Original file line number Diff line number Diff line change 15
15
"""
16
16
17
17
18
- def solution (n ) :
18
+ def solution (n : int ) -> int :
19
19
"""Returns the difference between the sum of the squares of the first n
20
20
natural numbers and the square of the sum.
21
21
@@ -36,4 +36,7 @@ def solution(n):
36
36
37
37
38
38
if __name__ == "__main__" :
39
+ import doctest
40
+
41
+ doctest .testmod ()
39
42
print (solution (int (input ("Enter a number: " ).strip ())))
You can’t perform that action at this time.
0 commit comments