mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
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:
parent
86fb2991d5
commit
2de2267319
|
@ -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
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
|
|
||||||
|
@ -28,14 +28,16 @@ def solution(n):
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
"""
|
"""
|
||||||
suma = 0
|
sum_of_squares = 0
|
||||||
sumb = 0
|
sum_of_ints = 0
|
||||||
for i in range(1, n + 1):
|
for i in range(1, n + 1):
|
||||||
suma += i ** 2
|
sum_of_squares += i ** 2
|
||||||
sumb += i
|
sum_of_ints += i
|
||||||
sum = sumb ** 2 - suma
|
return sum_of_ints ** 2 - sum_of_squares
|
||||||
return sum
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
print(solution(int(input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
|
|
@ -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
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
|
|
||||||
|
@ -28,11 +28,13 @@ def solution(n):
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
"""
|
"""
|
||||||
suma = n * (n + 1) / 2
|
sum_cubes = (n * (n + 1) // 2) ** 2
|
||||||
suma **= 2
|
sum_squares = n * (n + 1) * (2 * n + 1) // 6
|
||||||
sumb = n * (n + 1) * (2 * n + 1) / 6
|
return sum_cubes - sum_squares
|
||||||
return int(suma - sumb)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
print(solution(int(input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
|
|
@ -16,7 +16,7 @@ numbers and the square of the sum.
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: int) -> int:
|
||||||
"""Returns the difference between the sum of the squares of the first n
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
|
|
||||||
|
@ -35,4 +35,7 @@ def solution(n):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
print(solution(int(input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
|
|
@ -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
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
|
|
||||||
|
@ -36,4 +36,7 @@ def solution(n):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
print(solution(int(input("Enter a number: ").strip())))
|
print(solution(int(input("Enter a number: ").strip())))
|
||||||
|
|
18
project_euler/problem_06/test_solutions.py
Normal file
18
project_euler/problem_06/test_solutions.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from .sol1 import solution as sol1
|
||||||
|
from .sol2 import solution as sol2
|
||||||
|
from .sol3 import solution as sol3
|
||||||
|
from .sol4 import solution as sol4
|
||||||
|
|
||||||
|
|
||||||
|
def test_solutions() -> None:
|
||||||
|
"""
|
||||||
|
>>> test_solutions()
|
||||||
|
"""
|
||||||
|
assert sol1(10) == sol2(10) == sol3(10) == sol4(10) == 2640
|
||||||
|
assert sol1(15) == sol2(15) == sol3(15) == sol4(15) == 13160
|
||||||
|
assert sol1(20) == sol2(20) == sol3(20) == sol4(20) == 41230
|
||||||
|
assert sol1(50) == sol2(50) == sol3(50) == sol4(50) == 1582700
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_solutions()
|
Loading…
Reference in New Issue
Block a user