-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathprimes.py
210 lines (164 loc) · 3.38 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sys
_sieve_size = 0
bs = []
primes = []
def sieve(upperbound):
global _sieve_size, bs, primes
_sieve_size = upperbound+1
bs = [True] * 10000010
bs[0] = bs[1] = False
for i in range(2, _sieve_size):
if bs[i]:
for j in range(i*i, _sieve_size, i):
bs[j] = False
primes.append(i)
def isPrime(N):
global _sieve_size, primes
if N <= _sieve_size:
return bs[N]
for p in primes:
if p * p > N:
break
if N % p == 0:
return False
return True
def primeFactors(N):
global primes
factors = []
for p in primes:
if p * p > N:
break
while N % p == 0:
N //= p
factors.append(p)
if N != 1:
factors.append(N)
return factors
def numPF(N):
global primes
ans = 0
for p in primes:
if p * p > N:
break
while N % p == 0:
N //= p
ans += 1
if N != 1:
ans += 1
return ans
def numDiffPF(N):
global primes
ans = 0
for p in primes:
if p * p > N:
break
if N % p == 0:
ans += 1
while N % p == 0:
N //= p
if N != 1:
ans += 1
return ans
def sumPF(N):
global primes
ans = 0
for p in primes:
if p * p > N:
break
while N % p == 0:
N //= p
ans += p
if N != 1:
ans += N
return ans
def numDiv(N):
global primes
ans = 1
for p in primes:
if p * p > N:
break
power = 0
while N % p == 0:
N //= p
power += 1
ans = ans * (power + 1)
if N != 1:
return 2 * ans
else:
return ans
def sumDiv(N):
global primes
ans = 1
for p in primes:
if p * p > N:
break
multiplier = p
total = 1
while N % p == 0:
N //= p
total += multiplier
multiplier *= p
ans *= total
if N != 1:
ans *= N+1
return ans
def EulerPhi(N):
global primes
ans = N
for p in primes:
if p * p > N:
break
if N % p == 0:
ans -= ans // p
while N % p == 0:
N //= p
if N != 1:
ans -= ans // N
return ans
def main():
global primes
sieve(10000000)
print(primes[-1])
i = primes[-1]+1
while True:
if isPrime(i):
print('The next prime beyond the last prime generated is %d' % i)
break
i += 1
print(isPrime(2**31-1))
print(isPrime(136117223861))
print('')
r = primeFactors(2**31-1)
print(2**31-1)
print('\n'.join(['> '+str(val) for val in r]))
print('')
r = primeFactors(136117223861)
print(136117223861)
print('\n'.join(['> '+str(val) for val in r]))
print('')
r = primeFactors(5000000035)
print(5000000035)
print('\n'.join(['> '+str(val) for val in r]))
print('')
r = primeFactors(142391208960)
print(142391208960)
print('\n'.join(['> '+str(val) for val in r]))
print('')
r = primeFactors(100000380000361)
print(100000380000361)
print('\n'.join(['> '+str(val) for val in r]))
print('')
print('numPF(%d) = %d' % (60, numPF(60)))
print('numDiffPF(%d) = %d' % (60, numDiffPF(60)))
print('sumPF(%d) = %d' % (60, sumPF(60)))
print('numDiv(%d) = %d' % (60, numDiv(60)))
print('sumDiv(%d) = %d' % (60, sumDiv(60)))
print('EulerPhi(%d) = %d' % (36, EulerPhi(36)))
print();
print('numPF(%d) = %d' % (7, numPF(7)))
print('numDiffPF(%d) = %d' % (7, numDiffPF(7)))
print('sumPF(%d) = %d' % (7, sumPF(7)))
print('numDiv(%d) = %d' % (7, numDiv(7)))
print('sumDiv(%d) = %d' % (7, sumDiv(7)))
print('EulerPhi(%d) = %d' % (7, EulerPhi(7)))
main()