Skip to content

Commit 2de2267

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
Updated problem_06 in Project Euler (#2439)
* * rename variable * fix type hint * fix doctest * added test function * fixed import error * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 86fb299 commit 2de2267

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

Diff for: project_euler/problem_06/sol1.py

+9-7
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())))

Diff for: project_euler/problem_06/sol2.py

+7-5
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())))

Diff for: project_euler/problem_06/sol3.py

+4-1
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())))

Diff for: project_euler/problem_06/sol4.py

+4-1
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())))

Diff for: project_euler/problem_06/test_solutions.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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()

0 commit comments

Comments
 (0)