-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
403 lines (365 loc) · 9.22 KB
/
util.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def digits_10(n):
"""The number of digits in the decimal representation of any integer n, not
including the - sign.
>>> digits_10(0)
1
>>> digits_10(199)
3
>>> digits_10(-12)
2
>>> digits_10(0.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from math import floor, log10
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if n == 0:
return 1
return int(log10(abs(n))) + 1
def divisors(n):
"""A set of the proper divisors of n for any positive integer n.
>>> divisors(1)
{1}
>>> divisors(17.0)
{1}
>>> divisors(4) == {1, 2}
True
>>> divisors(49) == {1, 7}
True
>>> divisors(120) == {1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60}
True
>>> len(divisors(360))
23
>>> len(divisors(2520))
47
>>> divisors(0)
Traceback (most recent call last):
...
ValueError: n must be positive
>>> divisors(2**0.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from functools import reduce
from math import floor
from util import powerset, prime_factorization
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if not n >= 1:
raise ValueError("n must be positive")
pf = prime_factorization(n)
factor_sets = powerset(pf)
product = lambda factors: reduce(lambda x,y: x*y, factors, 1)
divisors = {product(factors) for factors in factor_sets}
if n != 1:
divisors.discard(n)
return divisors
def gcd(a, b):
"""Return the greatest common divisor of a and b for integers a and b not
both 0.
>>> gcd(54, 24)
6
>>> gcd(48, -180)
12
>>> gcd(-37, 600)
1
>>> gcd(-13, -13)
13
>>> gcd(20, 100)
20
>>> gcd(624129, 2061517)
18913
>>> gcd(0, 6)
6
>>> gcd(6, 0)
6
>>> gcd(0, 0)
Traceback (most recent call last):
...
ValueError: a or b must not be 0
>>> gcd(5.5, 11)
Traceback (most recent call last):
...
ValueError: a must be an integer
>>> gcd(0, 1.5)
Traceback (most recent call last):
...
ValueError: b must be an integer
"""
from math import floor
if floor(a) != a:
raise ValueError("a must be an integer")
a = floor(a)
if floor(b) != b:
raise ValueError("b must be an integer")
b = floor(b)
if a == 0 and b == 0:
raise ValueError("a or b must not be 0")
while b != 0:
a, b = b, a % b
return abs(a)
def is_pentagonal(n):
"""Return whether n is a pentagonal number for integer n >= 0.
n is a pentagonal number when sqrt(24n+1) = 5 (mod 6).
>>> is_pentagonal(0)
False
>>> is_pentagonal(1)
True
>>> is_pentagonal(925)
True
>>> is_pentagonal(1500002500000)
False
>>> is_pentagonal(1500002500001)
True
>>> is_pentagonal(-1)
Traceback (most recent call last):
...
ValueError: n must be non-negative
>>> is_pentagonal(2.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from math import floor, sqrt
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if not n >= 0:
raise ValueError("n must be non-negative")
s = sqrt(24*n + 1)
return s == floor(s) and s % 6 == 5
def is_prime(n):
"""Naive primality test. Return whether n is prime for integer n >= 2.
>>> is_prime(2)
True
>>> is_prime(4)
False
>>> is_prime(655559.0)
True
>>> is_prime(655559*655559)
False
>>> is_prime(1)
False
>>> is_prime(2.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from math import floor
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if not n >= 2:
return False
if n == 2:
return True
if n%2 == 0:
return False
for d in range(3, floor(n**0.5)+1, 2):
if n%d == 0:
return False
return True
def is_triangle(n):
"""Return whether n is a triangle number for integer n >= 0.
n is a triangle number iff 8n+1 is a square.
>>> is_triangle(0)
True
>>> is_triangle(1)
True
>>> is_triangle(16)
False
>>> is_triangle(15)
True
>>> is_triangle(500500)
True
>>> is_triangle(499513)
False
>>> is_triangle(-1)
Traceback (most recent call last):
...
ValueError: n must be non-negative
>>> is_triangle(2.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from math import floor, sqrt
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if not n >= 0:
raise ValueError("n must be non-negative")
x = 8 * n + 1
return sqrt(x) == floor(sqrt(x))
def lcm(a, b):
"""Return the least common multiple of a and b for integers and b not both
0.
>>> lcm(54, 24)
216
>>> lcm(48, -180)
720
>>> lcm(-37, 600)
22200
>>> lcm(-13, -13)
13
>>> lcm(20, 100)
100
>>> lcm(624129, 2061517)
68030061
>>> lcm(0, 6)
0
>>> lcm(6, 0)
0
>>> lcm(0, 0)
Traceback (most recent call last):
...
ValueError: a or b must not be 0
>>> lcm(5.5, 11)
Traceback (most recent call last):
...
ValueError: a must be an integer
>>> lcm(0, 1.5)
Traceback (most recent call last):
...
ValueError: b must be an integer
"""
return abs(a * b // gcd(a, b))
def phi(n):
"""Euler's totient function. Return phi(n) for integer n > 0.
>>> phi(1)
1
>>> phi(8)
4
>>> phi(19)
18
>>> phi(1234567890987654321)
668336784229323360
>>> phi(0)
Traceback (most recent call last):
...
ValueError: n must be positive
>>> phi(2.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from functools import reduce
from math import floor
return reduce(lambda acc, p: acc*(p-1)//p, prime_factors(n), n)
def postponed_sieve():
"""A prime generator from http://stackoverflow.com/a/10733621."""
from itertools import count
yield 2; yield 3; yield 5; yield 7;
sieve = {}
ps = postponed_sieve()
p = next(ps) and next(ps)
q = p*p
for c in count(9, 2):
if c in sieve:
s = sieve.pop(c)
elif c < q:
yield c
continue
else: # c == q
s = count(q + 2 * p, 2 * p)
p = next(ps)
q = p * p
for m in s:
if m not in sieve:
break;
sieve[m] = s
def powerset(iterable):
"""An iterable of iterables representing the powerset of the input iterable.
Implementation taken from:
https://docs.python.org/3/library/itertools.html#itertools-recipes
>>> list(powerset([]))
[()]
>>> list(powerset([0]))
[(), (0,)]
>>> list(powerset([0, 1]))
[(), (0,), (1,), (0, 1)]
"""
from itertools import chain, combinations
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def prime_factorization(n):
"""A sorted list of the prime factors of n for any positive integer n.
>>> prime_factorization(1)
[]
>>> prime_factorization(2)
[2]
>>> prime_factorization(17.0)
[17]
>>> prime_factorization(49)
[7, 7]
>>> prime_factorization(360)
[2, 2, 2, 3, 3, 5]
>>> prime_factorization(1234567890987654321)
[3, 3, 7, 19, 928163, 1111211111]
>>> prime_factorization(0)
Traceback (most recent call last):
...
ValueError: n must be positive
>>> prime_factorization(2**0.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
from math import floor
if floor(n) != n:
raise ValueError("n must be an integer")
n = floor(n)
if not n >= 1:
raise ValueError("n must be positive")
if n == 1:
return []
prime_factorization = []
d = 2
while d <= floor(n**0.5):
while n%d == 0:
prime_factorization.append(d)
n //= d
d += 1
if n > 1:
prime_factorization.append(n)
return prime_factorization
def prime_factors(n):
"""A set of the prime factors of n for any positive integer n.
>>> prime_factors(1)
set()
>>> prime_factors(2)
{2}
>>> prime_factors(17.0)
{17}
>>> prime_factors(49)
{7}
>>> prime_factors(360) == {2, 3, 5}
True
>>> prime_factors(1234567890987654321) == {3, 7, 19, 928163, 1111211111}
True
>>> prime_factors(0)
Traceback (most recent call last):
...
ValueError: n must be positive
>>> prime_factors(2**0.5)
Traceback (most recent call last):
...
ValueError: n must be an integer
"""
return set(prime_factorization(n))
primes = postponed_sieve
def primes_to(n):
"""Generate primes up to n."""
p = primes()
next_prime = next(p)
while next_prime <= n:
yield next_prime
next_prime = next(p)
if __name__ == "__main__":
import doctest
doctest.testmod()