-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathlib.py
38 lines (34 loc) · 1022 Bytes
/
mathlib.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
import functools
def _primes():
seq = []
yield 2
yield 3
i = 1
while True:
i += 4
if all(map(lambda x:i % x != 0, (el for el in seq if el <= i**0.5))):
yield i
seq.append(i)
i += 2
if all(map(lambda x:i % x != 0, (el for el in seq if el <= i**0.5))):
yield i
seq.append(i)
def primes(maximum=float('inf'), nb_max=float('inf')):
primesgenerator = _primes()
nextprime = next(primesgenerator)
nb = 0
while nb < nb_max and nextprime < maximum:
yield nextprime
nb += 1
nextprime = next(primesgenerator)
def factorize(integer):
factorization = []
primesgenerator = primes(integer+1)
while integer > 1:
nextprime = next(primesgenerator)
while integer % nextprime == 0:
integer = integer / nextprime
factorization.append(nextprime)
return factorization
def mul(aniterable):
return functools.reduce(lambda x,y:x*y, aniterable)