-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathprimes.py
More file actions
66 lines (58 loc) · 1.31 KB
/
primes.py
File metadata and controls
66 lines (58 loc) · 1.31 KB
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
#!/usr/bin/env python
# coding=utf-8
#
# Python Script
#
# Copyleft © Manoel Vilela
#
#
from sys import version_info
if version_info > (3, 0):
xrange = range
def primeGen(n):
for i in xrange(2, n):
prime = True
if i % 2 == 0 and i != 2:
continue
sqrtp = int(i ** 1 / 2)
for j in xrange(2, sqrtp):
if j % 2 == 0:
continue
if i % j == 0:
prime = False
break
if prime:
yield i
def primeGenEff(n):
pp = 2
yield pp
pp += 1
tp = [pp]
ss = [2]
while pp < n:
pp += ss[0]
test = True
sqrtpp = pp ** 1/2
for a in tp:
if a > sqrtpp:
break
if pp % a == 0:
test = False
break
if test:
tp.append(pp)
yield pp
def sieve5(n):
"""Return a list of the primes below n."""
prime = [True] * n
result = [2]
append = result.append
sqrt_n = (int(n ** .5) + 1) | 1 # ensure it's odd
for p in range(3, sqrt_n, 2):
if prime[p]:
append(p)
prime[p*p::2*p] = [False] * ((n - p*p - 1) // (2*p) + 1)
for p in range(sqrt_n, n, 2):
if prime[p]:
append(p)
return result