-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathPrime_number_decompositions.py
44 lines (35 loc) · 1 KB
/
Prime_number_decompositions.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
"""
Prime number decompositions
http://www.codewars.com/kata/prime-number-decompositions/train/python
"""
def getAllPrimeFactors(n):
if n == 1:
return [1]
result = []
if isvalidparameter(n):
factor = 2
while n > 1:
while n % factor == 0:
n /= factor
result.append(factor)
factor += 1
return result
def getUniquePrimeFactorsWithCount(n):
result = [[], []]
if isvalidparameter(n):
factors = getAllPrimeFactors(n)
for f in factors:
if f in result[0]:
result[1][-1] += 1
else:
result[0].append(f)
result[1].append(1)
return result
def getUniquePrimeFactorsWithProducts(n):
result = []
if isvalidparameter(n):
factors = getUniquePrimeFactorsWithCount(n)
result = map(lambda x: x[0] ** x[1], zip(factors[0], factors[1]))
return result
def isvalidparameter(n):
return isinstance(n, int) and n > 0