-
Notifications
You must be signed in to change notification settings - Fork 0
/
244.py
60 lines (45 loc) · 1.52 KB
/
244.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Problem:
The Sieve of Eratosthenes is an algorithm used to generate all prime numbers smaller
than N. The method is to take increasingly larger prime numbers, and mark their
multiples as composite.
For example, to find all primes less than 100, we would first mark [4, 6, 8, ...]
(multiples of two), then [6, 9, 12, ...] (multiples of three), and so on. Once we have
done this for all primes less than N, the unmarked numbers that remain will be prime.
Implement this algorithm.
Bonus: Create a generator that produces primes indefinitely (that is, without taking N
as an input).
"""
from typing import Generator, List
def sieve_of_eratosthenes(sieve: List[int] = []) -> List[int]:
if sieve:
length = len(sieve)
sieve.extend([True for _ in range(length)])
else:
length = 10
sieve = [True for _ in range(length * 2)]
sieve[0], sieve[1] = False, False
# sieve generation
for i in range(2, 2 * length):
if sieve[i]:
for j in range(2 * i, 2 * length, i):
sieve[j] = False
return sieve
def primes_generator() -> Generator[int, None, None]:
primes = sieve_of_eratosthenes()
prev = 0
while True:
for i in range(prev, len(primes)):
if primes[i]:
yield i
prev = len(primes)
primes = sieve_of_eratosthenes(primes)
if __name__ == "__main__":
generator = primes_generator()
for _ in range(35):
print(next(generator))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""