File tree 5 files changed +42
-14
lines changed
5 files changed +42
-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 ())))
Original file line number Diff line number Diff line change
1
+ from .sol1 import solution as sol1
2
+ from .sol2 import solution as sol2
3
+ from .sol3 import solution as sol3
4
+ from .sol4 import solution as sol4
5
+
6
+
7
+ def test_solutions () -> None :
8
+ """
9
+ >>> test_solutions()
10
+ """
11
+ assert sol1 (10 ) == sol2 (10 ) == sol3 (10 ) == sol4 (10 ) == 2640
12
+ assert sol1 (15 ) == sol2 (15 ) == sol3 (15 ) == sol4 (15 ) == 13160
13
+ assert sol1 (20 ) == sol2 (20 ) == sol3 (20 ) == sol4 (20 ) == 41230
14
+ assert sol1 (50 ) == sol2 (50 ) == sol3 (50 ) == sol4 (50 ) == 1582700
15
+
16
+
17
+ if __name__ == "__main__" :
18
+ test_solutions ()
You can’t perform that action at this time.
0 commit comments