Skip to content

Commit 53ff5d8

Browse files
committed
Apply review suggestions
1 parent 8f9d8ea commit 53ff5d8

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Diff for: project_euler/problem_234/sol1.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import math
2121

2222

23-
def sieve(n: int) -> list:
23+
def prime_sieve(n: int) -> list:
2424
"""
2525
Sieve of Erotosthenes
2626
Function to return all the prime numbers up to a certain number
2727
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
28-
>>> sieve(3)
28+
>>> prime_sieve(3)
2929
[2]
30-
>>> sieve(50)
30+
>>> prime_sieve(50)
3131
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
3232
"""
3333
is_prime = [True] * n
@@ -50,29 +50,34 @@ def sieve(n: int) -> list:
5050
return primes
5151

5252

53-
def solution() -> int:
53+
def solution(limit: int = 999_966_663_333) -> int:
5454
"""
55-
Computes the solution to the problem
56-
>>> solution()
57-
1259187438574927161
55+
Computes the solution to the problem up to the specified limit
56+
>>> solution(1000)
57+
34825
58+
59+
>>> solution(10_000)
60+
1134942
61+
62+
>>> solution(100_000)
63+
36393008
5864
"""
59-
limit = 999966663333
6065
primes_upper_bound = math.floor(math.sqrt(limit)) + 100
61-
primes = sieve(primes_upper_bound)
66+
primes = prime_sieve(primes_upper_bound)
6267

6368
matches_sum = 0
6469
prime_index = 0
6570
last_prime = primes[prime_index]
6671

67-
while (last_prime * last_prime) <= limit:
72+
while (last_prime ** 2) <= limit:
6873
next_prime = primes[prime_index + 1]
6974

70-
lower_bound = last_prime * last_prime
71-
upper_bound = next_prime * next_prime
75+
lower_bound = last_prime ** 2
76+
upper_bound = next_prime ** 2
7277

7378
# Get numbers divisible by lps(current)
7479
current = lower_bound + last_prime
75-
while current < upper_bound and current <= limit:
80+
while upper_bound > current <= limit:
7681
matches_sum += current
7782
current += last_prime
7883

@@ -87,8 +92,8 @@ def solution() -> int:
8792
current -= next_prime
8893

8994
# Remove the numbers divisible by both ups and lps
90-
current = lower_bound - lower_bound % (last_prime * next_prime)
91-
while current < upper_bound and current <= limit:
95+
current = 0
96+
while upper_bound > current <= limit:
9297
if current <= lower_bound:
9398
# Increment the current number
9499
current += last_prime * next_prime

0 commit comments

Comments
 (0)