From e258d368f99cfbd592db22a67f95555f2c9cd1b9 Mon Sep 17 00:00:00 2001 From: Hyftar Date: Wed, 7 Oct 2020 19:53:59 -0400 Subject: [PATCH 1/3] Add project euler problem 50 --- project_euler/problem_50/__init__.py | 0 project_euler/problem_50/sol1.py | 76 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 project_euler/problem_50/__init__.py create mode 100644 project_euler/problem_50/sol1.py diff --git a/project_euler/problem_50/__init__.py b/project_euler/problem_50/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py new file mode 100644 index 000000000000..6c6b8a838053 --- /dev/null +++ b/project_euler/problem_50/sol1.py @@ -0,0 +1,76 @@ +""" +https://projecteuler.net/problem=50 +Consecutive prime sum + +Problem 50 + +The prime 41, can be written as the sum of six consecutive primes: +41 = 2 + 3 + 5 + 7 + 11 + 13 + +This is the longest sum of consecutive primes that adds to a prime below +one-hundred. + +The longest sum of consecutive primes below one-thousand that adds to a prime, +contains 21 terms, and is equal to 953. + +Which prime, below one-million, can be written as the sum of the most +consecutive primes? +""" + + +def sieve(n: int) -> list: + """ + Sieve of Erotosthenes + Function to return all the prime numbers up to a certain number + https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + >>> sieve(3) + [2] + + >>> sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + """ + is_prime = [True] * n + is_prime[0] = False + is_prime[1] = False + is_prime[2] = True + + for i in range(3, int(n ** 0.5 + 1), 2): + index = i * 2 + while index < n: + is_prime[index] = False + index = index + i + + primes = [2] + + for i in range(3, n, 2): + if is_prime[i]: + primes.append(i) + + return primes + + +def solution() -> int: + """ + Returns the solution of the problem + >>> solution() + 997651 + """ + primes = sieve(1_000_000) + length = 0 + largest = 0 + + for i in range(len(primes)): + for j in range(i + length, len(primes)): + sol = sum(primes[i:j]) + if sol >= 1_000_000: + break + + if sol in primes: + length = j - i + largest = sol + + return largest + + +if __name__ == "__main__": + print(solution()) From 370cf6b1959858484aa72ec24d8ad664dc439362 Mon Sep 17 00:00:00 2001 From: Hyftar Date: Sat, 24 Oct 2020 16:25:35 -0400 Subject: [PATCH 2/3] Apply format changes --- .../{problem_50 => problem_050}/__init__.py | 0 .../{problem_50 => problem_050}/sol1.py | 27 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) rename project_euler/{problem_50 => problem_050}/__init__.py (100%) rename project_euler/{problem_50 => problem_050}/sol1.py (77%) diff --git a/project_euler/problem_50/__init__.py b/project_euler/problem_050/__init__.py similarity index 100% rename from project_euler/problem_50/__init__.py rename to project_euler/problem_050/__init__.py diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_050/sol1.py similarity index 77% rename from project_euler/problem_50/sol1.py rename to project_euler/problem_050/sol1.py index 6c6b8a838053..3f2981335955 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_050/sol1.py @@ -1,8 +1,7 @@ """ -https://projecteuler.net/problem=50 -Consecutive prime sum +Project Euler Problem 50: https://projecteuler.net/problem=50 -Problem 50 +Consecutive prime sum The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 @@ -49,20 +48,28 @@ def sieve(n: int) -> list: return primes -def solution() -> int: +def solution(ceiling: int = 1_000_000) -> int: """ - Returns the solution of the problem - >>> solution() - 997651 + Returns the biggest prime, below the celing, that can be written as the sum + of consecutive the most consecutive primes. + + >>> solution(500) + 499 + + >>> solution(1_000) + 953 + + >>> solution(10_000) + 9521 """ - primes = sieve(1_000_000) + primes = sieve(ceiling) length = 0 largest = 0 for i in range(len(primes)): for j in range(i + length, len(primes)): sol = sum(primes[i:j]) - if sol >= 1_000_000: + if sol >= ceiling: break if sol in primes: @@ -73,4 +80,4 @@ def solution() -> int: if __name__ == "__main__": - print(solution()) + print(f"{solution() = }") From e33d6bb40f19b1bab2f5e1a75e44dca2de070cd7 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Sun, 8 Nov 2020 07:59:21 +0530 Subject: [PATCH 3/3] Descriptive function/parameter name and type hints --- project_euler/problem_050/sol1.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_050/sol1.py b/project_euler/problem_050/sol1.py index 3f2981335955..7d142e5ffc91 100644 --- a/project_euler/problem_050/sol1.py +++ b/project_euler/problem_050/sol1.py @@ -15,33 +15,35 @@ Which prime, below one-million, can be written as the sum of the most consecutive primes? """ +from typing import List -def sieve(n: int) -> list: +def prime_sieve(limit: int) -> List[int]: """ Sieve of Erotosthenes - Function to return all the prime numbers up to a certain number + Function to return all the prime numbers up to a number 'limit' https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes - >>> sieve(3) + + >>> prime_sieve(3) [2] - >>> sieve(50) + >>> prime_sieve(50) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] """ - is_prime = [True] * n + is_prime = [True] * limit is_prime[0] = False is_prime[1] = False is_prime[2] = True - for i in range(3, int(n ** 0.5 + 1), 2): + for i in range(3, int(limit ** 0.5 + 1), 2): index = i * 2 - while index < n: + while index < limit: is_prime[index] = False index = index + i primes = [2] - for i in range(3, n, 2): + for i in range(3, limit, 2): if is_prime[i]: primes.append(i) @@ -62,7 +64,7 @@ def solution(ceiling: int = 1_000_000) -> int: >>> solution(10_000) 9521 """ - primes = sieve(ceiling) + primes = prime_sieve(ceiling) length = 0 largest = 0