Add default arguments for Project Euler problem 6 (#2957)

- Add default arguments to solution function
- Add link to Project Euler problem 6
- Add doctest for testing `solution()`
- Removed test_solutions.py as it is redundant
This commit is contained in:
Dhruv 2020-10-07 10:17:43 +05:30 committed by GitHub
parent 4d4ce400ec
commit ddf83ec886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 27 deletions

View File

@ -1,5 +1,5 @@
"""
Problem:
Problem 6: https://projecteuler.net/problem=6
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
@ -15,7 +15,7 @@ numbers and the square of the sum.
"""
def solution(n: int) -> int:
def solution(n: int = 100) -> int:
"""Returns the difference between the sum of the squares of the first n
natural numbers and the square of the sum.
@ -27,6 +27,8 @@ def solution(n: int) -> int:
41230
>>> solution(50)
1582700
>>> solution()
25164150
"""
sum_of_squares = 0
sum_of_ints = 0

View File

@ -1,5 +1,5 @@
"""
Problem:
Problem 6: https://projecteuler.net/problem=6
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
@ -15,7 +15,7 @@ numbers and the square of the sum.
"""
def solution(n: int) -> int:
def solution(n: int = 100) -> int:
"""Returns the difference between the sum of the squares of the first n
natural numbers and the square of the sum.
@ -27,6 +27,8 @@ def solution(n: int) -> int:
41230
>>> solution(50)
1582700
>>> solution()
25164150
"""
sum_cubes = (n * (n + 1) // 2) ** 2
sum_squares = n * (n + 1) * (2 * n + 1) // 6

View File

@ -1,5 +1,5 @@
"""
Problem:
Problem 6: https://projecteuler.net/problem=6
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
@ -16,7 +16,7 @@ numbers and the square of the sum.
import math
def solution(n: int) -> int:
def solution(n: int = 100) -> int:
"""Returns the difference between the sum of the squares of the first n
natural numbers and the square of the sum.
@ -28,6 +28,8 @@ def solution(n: int) -> int:
41230
>>> solution(50)
1582700
>>> solution()
25164150
"""
sum_of_squares = sum([i * i for i in range(1, n + 1)])
square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))

View File

@ -1,5 +1,5 @@
"""
Problem:
Problem 6: https://projecteuler.net/problem=6
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
@ -15,7 +15,7 @@ numbers and the square of the sum.
"""
def solution(n: int) -> int:
def solution(n: int = 100) -> int:
"""Returns the difference between the sum of the squares of the first n
natural numbers and the square of the sum.
@ -27,7 +27,7 @@ def solution(n: int) -> int:
41230
>>> solution(50)
1582700
>>> solution(100)
>>> solution()
25164150
"""
sum_of_squares = n * (n + 1) * (2 * n + 1) / 6

View File

@ -1,18 +0,0 @@
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()