mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Fix style of the first ten solutions for Project Euler (#3242)
* Fix style of the first ten solutions for Project Euler - Unify the header docstring, and add reference URLs to wikipedia or similar - Fix docstrings to be properly multilined - Add newlines where appropriate - Add doctests where they were missing - Remove doctests that test for the correct solution - fix obvious spelling or grammar mistakes in comments and exception messages - Fix line endings to be UNIX. This makes two of the files seem to have changed completely - no functional changes in any of the solutions were done (except for the spelling fixes mentioned above) * Fix docstrings and main function as per Style Guide
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
"""
|
||||
https://projecteuler.net/problem=10
|
||||
Project Euler Problem 10: https://projecteuler.net/problem=10
|
||||
|
||||
Summation of primes
|
||||
|
||||
Problem Statement:
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million.
|
||||
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
"""
|
||||
|
||||
from math import sqrt
|
||||
|
||||
|
||||
def is_prime(n: int) -> bool:
|
||||
"""Returns boolean representing primality of given number num.
|
||||
"""
|
||||
Returns boolean representing primality of given number num.
|
||||
|
||||
>>> is_prime(2)
|
||||
True
|
||||
>>> is_prime(3)
|
||||
@@ -20,6 +27,7 @@ def is_prime(n: int) -> bool:
|
||||
>>> is_prime(2999)
|
||||
True
|
||||
"""
|
||||
|
||||
for i in range(2, int(sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
@@ -28,11 +36,9 @@ def is_prime(n: int) -> bool:
|
||||
|
||||
|
||||
def solution(n: int = 2000000) -> int:
|
||||
"""Returns the sum of all the primes below n.
|
||||
"""
|
||||
Returns the sum of all the primes below n.
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution(2000000)
|
||||
# 142913828922
|
||||
>>> solution(1000)
|
||||
76127
|
||||
>>> solution(5000)
|
||||
@@ -42,6 +48,7 @@ def solution(n: int = 2000000) -> int:
|
||||
>>> solution(7)
|
||||
10
|
||||
"""
|
||||
|
||||
if n > 2:
|
||||
sum_of_primes = 2
|
||||
else:
|
||||
@@ -55,4 +62,4 @@ def solution(n: int = 2000000) -> int:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(input().strip())))
|
||||
print(f"{solution() = }")
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
"""
|
||||
https://projecteuler.net/problem=10
|
||||
Project Euler Problem 10: https://projecteuler.net/problem=10
|
||||
|
||||
Summation of primes
|
||||
|
||||
Problem Statement:
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million.
|
||||
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
"""
|
||||
import math
|
||||
from itertools import takewhile
|
||||
@@ -12,7 +16,9 @@ from typing import Iterator
|
||||
|
||||
|
||||
def is_prime(number: int) -> bool:
|
||||
"""Returns boolean representing primality of given number num.
|
||||
"""
|
||||
Returns boolean representing primality of given number num.
|
||||
|
||||
>>> is_prime(2)
|
||||
True
|
||||
>>> is_prime(3)
|
||||
@@ -22,12 +28,17 @@ def is_prime(number: int) -> bool:
|
||||
>>> is_prime(2999)
|
||||
True
|
||||
"""
|
||||
|
||||
if number % 2 == 0 and number > 2:
|
||||
return False
|
||||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
||||
|
||||
|
||||
def prime_generator() -> Iterator[int]:
|
||||
"""
|
||||
Generate a list sequence of prime numbers
|
||||
"""
|
||||
|
||||
num = 2
|
||||
while True:
|
||||
if is_prime(num):
|
||||
@@ -36,11 +47,9 @@ def prime_generator() -> Iterator[int]:
|
||||
|
||||
|
||||
def solution(n: int = 2000000) -> int:
|
||||
"""Returns the sum of all the primes below n.
|
||||
"""
|
||||
Returns the sum of all the primes below n.
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution(2000000)
|
||||
# 142913828922
|
||||
>>> solution(1000)
|
||||
76127
|
||||
>>> solution(5000)
|
||||
@@ -50,8 +59,9 @@ def solution(n: int = 2000000) -> int:
|
||||
>>> solution(7)
|
||||
10
|
||||
"""
|
||||
|
||||
return sum(takewhile(lambda x: x < n, prime_generator()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(input().strip())))
|
||||
print(f"{solution() = }")
|
||||
|
||||
@@ -1,43 +1,47 @@
|
||||
"""
|
||||
https://projecteuler.net/problem=10
|
||||
Project Euler Problem 10: https://projecteuler.net/problem=10
|
||||
|
||||
Summation of primes
|
||||
|
||||
Problem Statement:
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million.
|
||||
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
- https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
"""
|
||||
|
||||
|
||||
def solution(n: int = 2000000) -> int:
|
||||
"""Returns the sum of all the primes below n using Sieve of Eratosthenes:
|
||||
"""
|
||||
Returns the sum of all the primes below n using Sieve of Eratosthenes:
|
||||
|
||||
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
The sieve of Eratosthenes is one of the most efficient ways to find all primes
|
||||
smaller than n when n is smaller than 10 million. Only for positive numbers.
|
||||
|
||||
>>> solution(2_000_000)
|
||||
142913828922
|
||||
>>> solution(1_000)
|
||||
>>> solution(1000)
|
||||
76127
|
||||
>>> solution(5_000)
|
||||
>>> solution(5000)
|
||||
1548136
|
||||
>>> solution(10_000)
|
||||
>>> solution(10000)
|
||||
5736396
|
||||
>>> solution(7)
|
||||
10
|
||||
>>> solution(7.1) # doctest: +ELLIPSIS
|
||||
>>> solution(7.1) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'float' object cannot be interpreted as an integer
|
||||
>>> solution(-7) # doctest: +ELLIPSIS
|
||||
>>> solution(-7) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: list assignment index out of range
|
||||
>>> solution("seven") # doctest: +ELLIPSIS
|
||||
>>> solution("seven") # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: can only concatenate str (not "int") to str
|
||||
"""
|
||||
|
||||
primality_list = [0 for i in range(n + 1)]
|
||||
primality_list[0] = 1
|
||||
primality_list[1] = 1
|
||||
@@ -54,4 +58,4 @@ def solution(n: int = 2000000) -> int:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(input().strip())))
|
||||
print(f"{solution() = }")
|
||||
|
||||
Reference in New Issue
Block a user