Skip to content

Instantly share code, notes, and snippets.

@back-seat-driver
Last active July 25, 2017 22:18
Show Gist options
  • Save back-seat-driver/6f2dfc3d1744040af62dae8219d7e69b to your computer and use it in GitHub Desktop.
Save back-seat-driver/6f2dfc3d1744040af62dae8219d7e69b to your computer and use it in GitHub Desktop.
Prime Factorization Algorithm
from Primes_Under_1_000_000 import prime_list
'''
To return the prime factorization of a number
'''
def is_div(v,n):
if v%n==0:
return(True)
return(False)
def div_return(n,d):
return(n//d)
def prime_factor(n):
index = 0
initial = int(n)
prime_factors=[]
while initial != 1:
if is_div(initial,prime_list[index])==True:
prime_factors.append(prime_list[index])
initial = div_return(initial,prime_list[index])
else:
index+=1
return(prime_factors)
'''
To return the first number with a specified number of factors
'''
def race_factors(n):
prime_factors = map(lambda element: element-1, sorted(prime_factor(n),reverse=True))
value = 1
for i in range(len(prime_factors)):
value*=prime_list[i]**prime_factors[i]
return(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment