mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
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:
parent
4d4ce400ec
commit
ddf83ec886
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Problem:
|
Problem 6: https://projecteuler.net/problem=6
|
||||||
|
|
||||||
The sum of the squares of the first ten natural numbers is,
|
The sum of the squares of the first ten natural numbers is,
|
||||||
1^2 + 2^2 + ... + 10^2 = 385
|
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
|
"""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.
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ def solution(n: int) -> int:
|
||||||
41230
|
41230
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
|
>>> solution()
|
||||||
|
25164150
|
||||||
"""
|
"""
|
||||||
sum_of_squares = 0
|
sum_of_squares = 0
|
||||||
sum_of_ints = 0
|
sum_of_ints = 0
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Problem:
|
Problem 6: https://projecteuler.net/problem=6
|
||||||
|
|
||||||
The sum of the squares of the first ten natural numbers is,
|
The sum of the squares of the first ten natural numbers is,
|
||||||
1^2 + 2^2 + ... + 10^2 = 385
|
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
|
"""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.
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ def solution(n: int) -> int:
|
||||||
41230
|
41230
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
|
>>> solution()
|
||||||
|
25164150
|
||||||
"""
|
"""
|
||||||
sum_cubes = (n * (n + 1) // 2) ** 2
|
sum_cubes = (n * (n + 1) // 2) ** 2
|
||||||
sum_squares = n * (n + 1) * (2 * n + 1) // 6
|
sum_squares = n * (n + 1) * (2 * n + 1) // 6
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Problem:
|
Problem 6: https://projecteuler.net/problem=6
|
||||||
|
|
||||||
The sum of the squares of the first ten natural numbers is,
|
The sum of the squares of the first ten natural numbers is,
|
||||||
1^2 + 2^2 + ... + 10^2 = 385
|
1^2 + 2^2 + ... + 10^2 = 385
|
||||||
|
@ -16,7 +16,7 @@ numbers and the square of the sum.
|
||||||
import math
|
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
|
"""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,6 +28,8 @@ def solution(n: int) -> int:
|
||||||
41230
|
41230
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
|
>>> solution()
|
||||||
|
25164150
|
||||||
"""
|
"""
|
||||||
sum_of_squares = sum([i * i for i in range(1, n + 1)])
|
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))
|
square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Problem:
|
Problem 6: https://projecteuler.net/problem=6
|
||||||
|
|
||||||
The sum of the squares of the first ten natural numbers is,
|
The sum of the squares of the first ten natural numbers is,
|
||||||
1^2 + 2^2 + ... + 10^2 = 385
|
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
|
"""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.
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ def solution(n: int) -> int:
|
||||||
41230
|
41230
|
||||||
>>> solution(50)
|
>>> solution(50)
|
||||||
1582700
|
1582700
|
||||||
>>> solution(100)
|
>>> solution()
|
||||||
25164150
|
25164150
|
||||||
"""
|
"""
|
||||||
sum_of_squares = n * (n + 1) * (2 * n + 1) / 6
|
sum_of_squares = n * (n + 1) * (2 * n + 1) / 6
|
||||||
|
|
|
@ -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()
|
|
Loading…
Reference in New Issue
Block a user