Skip to content

Commit 7c7e6a4

Browse files
* rename variable
* fix type hint * fix doctest
1 parent cbbc43b commit 7c7e6a4

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

project_euler/problem_06/sol1.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717

18-
def solution(n):
18+
def solution(n: int) -> int:
1919
"""Returns the difference between the sum of the squares of the first n
2020
natural numbers and the square of the sum.
2121
@@ -28,14 +28,16 @@ def solution(n):
2828
>>> solution(50)
2929
1582700
3030
"""
31-
suma = 0
32-
sumb = 0
31+
sum_of_squares = 0
32+
sum_of_ints = 0
3333
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
3837

3938

4039
if __name__ == "__main__":
40+
import doctest
41+
42+
doctest.testmod()
4143
print(solution(int(input().strip())))

project_euler/problem_06/sol2.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717

18-
def solution(n):
18+
def solution(n: int) -> int:
1919
"""Returns the difference between the sum of the squares of the first n
2020
natural numbers and the square of the sum.
2121
@@ -28,11 +28,13 @@ def solution(n):
2828
>>> solution(50)
2929
1582700
3030
"""
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
3534

3635

3736
if __name__ == "__main__":
37+
import doctest
38+
39+
doctest.testmod()
3840
print(solution(int(input().strip())))

project_euler/problem_06/sol3.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import math
1717

1818

19-
def solution(n):
19+
def solution(n: int) -> int:
2020
"""Returns the difference between the sum of the squares of the first n
2121
natural numbers and the square of the sum.
2222
@@ -35,4 +35,7 @@ def solution(n):
3535

3636

3737
if __name__ == "__main__":
38+
import doctest
39+
40+
doctest.testmod()
3841
print(solution(int(input().strip())))

project_euler/problem_06/sol4.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717

18-
def solution(n):
18+
def solution(n: int) -> int:
1919
"""Returns the difference between the sum of the squares of the first n
2020
natural numbers and the square of the sum.
2121
@@ -36,4 +36,7 @@ def solution(n):
3636

3737

3838
if __name__ == "__main__":
39+
import doctest
40+
41+
doctest.testmod()
3942
print(solution(int(input("Enter a number: ").strip())))

0 commit comments

Comments
 (0)