20
20
import math
21
21
22
22
23
- def sieve (n : int ) -> list :
23
+ def prime_sieve (n : int ) -> list :
24
24
"""
25
25
Sieve of Erotosthenes
26
26
Function to return all the prime numbers up to a certain number
27
27
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
28
- >>> sieve (3)
28
+ >>> prime_sieve (3)
29
29
[2]
30
- >>> sieve (50)
30
+ >>> prime_sieve (50)
31
31
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
32
32
"""
33
33
is_prime = [True ] * n
@@ -50,29 +50,34 @@ def sieve(n: int) -> list:
50
50
return primes
51
51
52
52
53
- def solution () -> int :
53
+ def solution (limit : int = 999_966_663_333 ) -> int :
54
54
"""
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
58
64
"""
59
- limit = 999966663333
60
65
primes_upper_bound = math .floor (math .sqrt (limit )) + 100
61
- primes = sieve (primes_upper_bound )
66
+ primes = prime_sieve (primes_upper_bound )
62
67
63
68
matches_sum = 0
64
69
prime_index = 0
65
70
last_prime = primes [prime_index ]
66
71
67
- while (last_prime * last_prime ) <= limit :
72
+ while (last_prime ** 2 ) <= limit :
68
73
next_prime = primes [prime_index + 1 ]
69
74
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
72
77
73
78
# Get numbers divisible by lps(current)
74
79
current = lower_bound + last_prime
75
- while current < upper_bound and current <= limit :
80
+ while upper_bound > current <= limit :
76
81
matches_sum += current
77
82
current += last_prime
78
83
@@ -87,8 +92,8 @@ def solution() -> int:
87
92
current -= next_prime
88
93
89
94
# 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 :
92
97
if current <= lower_bound :
93
98
# Increment the current number
94
99
current += last_prime * next_prime
0 commit comments