File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -1030,13 +1030,16 @@ The following recipes have a more mathematical flavor:
1030
1030
def sieve(n):
1031
1031
"Primes less than n."
1032
1032
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
1033
+ if n > 2:
1034
+ yield 2
1035
+ start = 3
1033
1036
data = bytearray((0, 1)) * (n // 2)
1034
- data[:3] = 0, 0, 0
1035
1037
limit = math.isqrt(n) + 1
1036
- for p in compress(range(limit), data):
1038
+ for p in iter_index(data, 1, start, limit):
1039
+ yield from iter_index(data, 1, start, p*p)
1037
1040
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
1038
- data[2] = 1
1039
- return iter_index(data, 1) if n > 2 else iter([] )
1041
+ start = p*p
1042
+ yield from iter_index(data, 1, start )
1040
1043
1041
1044
def factor(n):
1042
1045
"Prime factors of n."
You can’t perform that action at this time.
0 commit comments