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>
This commit is contained in:
Du Yuanchao
2020-09-17 17:37:53 +08:00
committed by GitHub
parent 86fb2991d5
commit 2de2267319
5 changed files with 42 additions and 14 deletions

View File

@ -15,7 +15,7 @@ numbers and the square of the sum.
"""
def solution(n):
def solution(n: int) -> int:
"""Returns the difference between the sum of the squares of the first n
natural numbers and the square of the sum.
@ -28,11 +28,13 @@ def solution(n):
>>> solution(50)
1582700
"""
suma = n * (n + 1) / 2
suma **= 2
sumb = n * (n + 1) * (2 * n + 1) / 6
return int(suma - sumb)
sum_cubes = (n * (n + 1) // 2) ** 2
sum_squares = n * (n + 1) * (2 * n + 1) // 6
return sum_cubes - sum_squares
if __name__ == "__main__":
import doctest
doctest.testmod()
print(solution(int(input().strip())))