Updated problem_06 in Project Euler (#2439) · programmin1/Python@2de2267 · GitHub
Skip to content

Commit 2de2267

Browse files
realDuYuanChaogithub-actions
andauthored
Updated problem_06 in Project Euler (TheAlgorithms#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

5 files changed

Lines changed: 42 additions & 14 deletions

File tree

project_euler/problem_06/sol1.py

Lines changed: 9 additions & 7 deletions

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())))
Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)