-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.py
66 lines (59 loc) · 1.65 KB
/
primes.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
61
62
63
64
65
66
import sys
import math
class PrimeGenerator:
def __init__(self):
self.until = 2
self.primes = set()
self.dico = {}
def is_prime(self, n: int) -> bool:
if n > self.until:
self.compute_until(n)
return n in self.primes
def primes_until(self, n: int):
if n > self.until:
self.compute_until(n)
return self.primes
def compute_until(self, until: int):
if until < self.until:
pass
i = self.until
while i <= until:
if i in self.dico:
for p in self.dico[i]:
tmp = p+i
if tmp not in self.dico:
self.dico[tmp] = [p]
else:
self.dico[tmp].append(p)
del self.dico[i]
else:
self.primes.add(i)
tmp = 2*i
if tmp not in self.dico:
self.dico[tmp] = [i]
else:
self.dico[tmp].append(i)
i += 1
self.until = until
def decompose(self, n: int):
if n > self.until:
self.compute_until(n)
ans = []
for i in self.primes:
curr = 0
while n % i == 0:
curr += 1
n = n // i
if curr > 0:
ans.append((i, curr))
if n == 1:
break
return ans
def __iter__(self):
for p in self.primes:
yield p
i = self.n
while True:
if is_prime(i):
yield i
i += 1