mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-20 05:29:48 +00:00
Project Euler Problem 10: style improvements (#2924)
Rename the main solution functions to solution. Rename prime chec functions to is_prime. Add default args, typehints, expand variable names.
This commit is contained in:
parent
000cedc07f
commit
e74adc4a6d
@ -1,4 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
|
https://projecteuler.net/problem=10
|
||||||
|
|
||||||
Problem Statement:
|
Problem Statement:
|
||||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||||
|
|
||||||
@ -7,7 +9,17 @@ Find the sum of all the primes below two million.
|
|||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n):
|
def is_prime(n: int) -> bool:
|
||||||
|
"""Returns boolean representing primality of given number num.
|
||||||
|
>>> is_prime(2)
|
||||||
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
"""
|
||||||
for i in range(2, int(sqrt(n)) + 1):
|
for i in range(2, int(sqrt(n)) + 1):
|
||||||
if n % i == 0:
|
if n % i == 0:
|
||||||
return False
|
return False
|
||||||
@ -15,20 +27,7 @@ def is_prime(n):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def sum_of_primes(n):
|
def solution(n: int = 2000000) -> int:
|
||||||
if n > 2:
|
|
||||||
sumOfPrimes = 2
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
for i in range(3, n, 2):
|
|
||||||
if is_prime(i):
|
|
||||||
sumOfPrimes += i
|
|
||||||
|
|
||||||
return sumOfPrimes
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
|
||||||
"""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.
|
# The code below has been commented due to slow execution affecting Travis.
|
||||||
@ -43,7 +42,16 @@ def solution(n):
|
|||||||
>>> solution(7)
|
>>> solution(7)
|
||||||
10
|
10
|
||||||
"""
|
"""
|
||||||
return sum_of_primes(n)
|
if n > 2:
|
||||||
|
sum_of_primes = 2
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for i in range(3, n, 2):
|
||||||
|
if is_prime(i):
|
||||||
|
sum_of_primes += i
|
||||||
|
|
||||||
|
return sum_of_primes
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
|
https://projecteuler.net/problem=10
|
||||||
|
|
||||||
Problem Statement:
|
Problem Statement:
|
||||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||||
|
|
||||||
@ -6,23 +8,34 @@ Find the sum of all the primes below two million.
|
|||||||
"""
|
"""
|
||||||
import math
|
import math
|
||||||
from itertools import takewhile
|
from itertools import takewhile
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
|
|
||||||
def primeCheck(number):
|
def is_prime(number: int) -> bool:
|
||||||
|
"""Returns boolean representing primality of given number num.
|
||||||
|
>>> is_prime(2)
|
||||||
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
"""
|
||||||
if number % 2 == 0 and number > 2:
|
if number % 2 == 0 and number > 2:
|
||||||
return False
|
return False
|
||||||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
||||||
|
|
||||||
|
|
||||||
def prime_generator():
|
def prime_generator() -> Iterator[int]:
|
||||||
num = 2
|
num = 2
|
||||||
while True:
|
while True:
|
||||||
if primeCheck(num):
|
if is_prime(num):
|
||||||
yield num
|
yield num
|
||||||
num += 1
|
num += 1
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
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.
|
# The code below has been commented due to slow execution affecting Travis.
|
||||||
|
@ -4,55 +4,54 @@ https://projecteuler.net/problem=10
|
|||||||
Problem Statement:
|
Problem Statement:
|
||||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||||
|
|
||||||
Find the sum of all the primes below two million using Sieve_of_Eratosthenes:
|
Find the sum of all the primes below two million.
|
||||||
|
|
||||||
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.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def prime_sum(n: int) -> 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 using Sieve of Eratosthenes:
|
||||||
|
|
||||||
>>> prime_sum(2_000_000)
|
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
|
142913828922
|
||||||
>>> prime_sum(1_000)
|
>>> solution(1_000)
|
||||||
76127
|
76127
|
||||||
>>> prime_sum(5_000)
|
>>> solution(5_000)
|
||||||
1548136
|
1548136
|
||||||
>>> prime_sum(10_000)
|
>>> solution(10_000)
|
||||||
5736396
|
5736396
|
||||||
>>> prime_sum(7)
|
>>> solution(7)
|
||||||
10
|
10
|
||||||
>>> prime_sum(7.1) # doctest: +ELLIPSIS
|
>>> solution(7.1) # doctest: +ELLIPSIS
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: 'float' object cannot be interpreted as an integer
|
TypeError: 'float' object cannot be interpreted as an integer
|
||||||
>>> prime_sum(-7) # doctest: +ELLIPSIS
|
>>> solution(-7) # doctest: +ELLIPSIS
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
IndexError: list assignment index out of range
|
IndexError: list assignment index out of range
|
||||||
>>> prime_sum("seven") # doctest: +ELLIPSIS
|
>>> solution("seven") # doctest: +ELLIPSIS
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: can only concatenate str (not "int") to str
|
TypeError: can only concatenate str (not "int") to str
|
||||||
"""
|
"""
|
||||||
list_ = [0 for i in range(n + 1)]
|
primality_list = [0 for i in range(n + 1)]
|
||||||
list_[0] = 1
|
primality_list[0] = 1
|
||||||
list_[1] = 1
|
primality_list[1] = 1
|
||||||
|
|
||||||
for i in range(2, int(n ** 0.5) + 1):
|
for i in range(2, int(n ** 0.5) + 1):
|
||||||
if list_[i] == 0:
|
if primality_list[i] == 0:
|
||||||
for j in range(i * i, n + 1, i):
|
for j in range(i * i, n + 1, i):
|
||||||
list_[j] = 1
|
primality_list[j] = 1
|
||||||
s = 0
|
sum_of_primes = 0
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
if list_[i] == 0:
|
if primality_list[i] == 0:
|
||||||
s += i
|
sum_of_primes += i
|
||||||
return s
|
return sum_of_primes
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# import doctest
|
print(solution(int(input().strip())))
|
||||||
# doctest.testmod()
|
|
||||||
print(prime_sum(int(input().strip())))
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user