Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

illustrations for RH book

Views: 1857
1
def mult_parities(int bound, bint verbose=False):
2
"""
3
Return list v of length bound such that v[n] is 0 if n is
4
multiplicative even, and v[n] is 1 if n is multiplicatively odd.
5
Also v[0] is None.
6
7
This goes up to bound=`10^7` in about 2 seconds, but uses a lot of
8
memory beyond this size. On a machine with sufficient `10^8`
9
takes around 20 seconds. If you have about 20-30GB of RAM, you can
10
go to `10^9` in about 6 minutes; this is far enough to witness the
11
first crossover point.
12
"""
13
from sage.all import log, floor, prime_range
14
cdef int i, j, k, n, p, last_len, cur_ptr, last_parity, cur_parity
15
cdef long long m
16
17
cdef int* v = <int*> sage_malloc(sizeof(int)*bound)
18
if not v: raise MemoryError
19
20
cdef int* last = <int*> sage_malloc(sizeof(int)*bound)
21
if not last: raise MemoryError
22
23
cdef int* cur = <int*> sage_malloc(sizeof(int)*bound)
24
if not cur: raise MemoryError
25
26
for i in range(bound):
27
v[i] = -1
28
29
v[1] = 0
30
31
P = prime_range(bound)
32
cdef int* primes = <int*> sage_malloc(sizeof(int)*len(P))
33
if not primes: raise MemoryError
34
for i in range(len(P)):
35
primes[i] = P[i]
36
v[primes[i]] = 1
37
last[i] = primes[i]
38
39
cdef int len_P = len(P)
40
last_len = len_P
41
last_parity = 1
42
cdef int loops = floor(log(bound,2))+1
43
for k in range(loops):
44
cur_ptr = 0
45
cur_parity = (last_parity+1)%2
46
if verbose:
47
print "loop %s (of %s); last = %s"%(k,loops, last_len)
48
_sig_on
49
for n in range(last_len):
50
for j in range(len_P):
51
m = (<long long> last[n]) * (<long long> primes[j])
52
if m >= bound:
53
break
54
if v[m] == -1:
55
v[m] = cur_parity
56
cur[cur_ptr] = m
57
cur_ptr+=1
58
_sig_off
59
last_parity = cur_parity
60
last_len = cur_ptr
61
_sig_on
62
for n in range(last_len):
63
last[n] = cur[n]
64
_sig_off
65
66
ans = [v[i] for i in range(bound)]
67
sage_free(v)
68
sage_free(last)
69
sage_free(cur)
70
sage_free(primes)
71
ans[0] = None
72
return ans
73
74