Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241818 views
1
#################################################################################
2
#
3
# (c) Copyright 2011 William Stein
4
#
5
# This file is part of PSAGE.
6
#
7
# PSAGE is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 2 of the License, or
10
# (at your option) any later version.
11
#
12
# PSAGE is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
#################################################################################
21
22
include "stdsage.pxi"
23
include "interrupt.pxi"
24
25
def mult_parities(int bound, bint verbose=False):
26
"""
27
Return list v of length bound such that v[n] is 0 if n is
28
multiplicative even, and v[n] is 1 if n is multiplicatively odd.
29
Also v[0] is None.
30
31
This goes up to bound=`10^7` in about 2 seconds, but uses a lot of
32
memory beyond this size. On a machine with sufficient `10^8`
33
takes around 20 seconds. If you have about 20-30GB of RAM, you can
34
go to `10^9` in about 6 minutes; this is far enough to witness the
35
first crossover point.
36
"""
37
from sage.all import log, floor, prime_range
38
cdef int i, j, k, n, p, last_len, cur_ptr, last_parity, cur_parity
39
cdef long long m
40
41
cdef int* v = <int*> sage_malloc(sizeof(int)*bound)
42
if not v: raise MemoryError
43
44
cdef int* last = <int*> sage_malloc(sizeof(int)*bound)
45
if not last: raise MemoryError
46
47
cdef int* cur = <int*> sage_malloc(sizeof(int)*bound)
48
if not cur: raise MemoryError
49
50
for i in range(bound):
51
v[i] = -1
52
53
v[1] = 0
54
55
P = prime_range(bound)
56
cdef int* primes = <int*> sage_malloc(sizeof(int)*len(P))
57
if not primes: raise MemoryError
58
for i in range(len(P)):
59
primes[i] = P[i]
60
v[primes[i]] = 1
61
last[i] = primes[i]
62
63
cdef int len_P = len(P)
64
last_len = len_P
65
last_parity = 1
66
cdef int loops = floor(log(bound,2))+1
67
for k in range(loops):
68
cur_ptr = 0
69
cur_parity = (last_parity+1)%2
70
if verbose:
71
print "loop %s (of %s); last = %s"%(k,loops, last_len)
72
_sig_on
73
for n in range(last_len):
74
for j in range(len_P):
75
m = (<long long> last[n]) * (<long long> primes[j])
76
if m >= bound:
77
break
78
if v[m] == -1:
79
v[m] = cur_parity
80
cur[cur_ptr] = m
81
cur_ptr+=1
82
_sig_off
83
last_parity = cur_parity
84
last_len = cur_ptr
85
_sig_on
86
for n in range(last_len):
87
last[n] = cur[n]
88
_sig_off
89
90
ans = [v[i] for i in range(bound)]
91
sage_free(v)
92
sage_free(last)
93
sage_free(cur)
94
sage_free(primes)
95
ans[0] = None
96
return ans
97
98