Created
May 8, 2012 03:37
-
-
Save dzhou/2632362 to your computer and use it in GitHub Desktop.
fast prime/factorization mathematics in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Kefei Dan Zhou | |
# | |
import math | |
# return a dict or a list of primes up to N | |
# create full prime sieve for N=10^6 in 1 sec | |
def prime_sieve(n, output={}): | |
nroot = int(math.sqrt(n)) | |
sieve = range(n+1) | |
sieve[1] = 0 | |
for i in xrange(2, nroot+1): | |
if sieve[i] != 0: | |
m = n/i - i | |
sieve[i*i: n+1:i] = [0] * (m+1) | |
if type(output) == dict: | |
pmap = {} | |
for x in sieve: | |
if x != 0: | |
pmap[x] = True | |
return pmap | |
elif type(output) == list: | |
return [x for x in sieve if x != 0] | |
else: | |
return None | |
# get a list of all factors for N | |
# ex: get_factors(10) -> [1,2,5,10] | |
def get_factors(n, primelist=None): | |
if primelist is None: | |
primelist = prime_sieve(n,output=[]) | |
fcount = {} | |
for p in primelist: | |
if p > n: | |
break | |
if n % p == 0: | |
fcount[p] = 0 | |
while n % p == 0: | |
n /= p | |
fcount[p] += 1 | |
factors = [1] | |
for i in fcount: | |
level = [] | |
exp = [i**(x+1) for x in range(fcount[i])] | |
for j in exp: | |
level.extend([j*x for x in factors]) | |
factors.extend(level) | |
return factors | |
# get a list of prime factors | |
# ex: get_prime_factors(140) -> ((2,2), (5,1), (7,1)) | |
# 140 = 2^2 * 5^1 * 7^1 | |
def get_prime_factors(n, primelist): | |
if primelist is None: | |
primelist = prime_sieve(n,output=[]) | |
fs = [] | |
for p in primelist: | |
count = 0 | |
while n % p == 0: | |
n /= p | |
count += 1 | |
if count > 0: | |
fs.append((p, count)) | |
return fs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please consider updating it for Python3?