-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_factoring.py
29 lines (26 loc) · 1000 Bytes
/
prime_factoring.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
# Calculate the prime factors of a number. Level: 5 kyu. This took me many hours (10 hours) to figure out. I think I need to take an online course on algorithms.
# description: https://www.codewars.com/kata/primes-in-numbers/train/python
def primeFactors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n) # Find prime factors and create a list
prime_dict = {}
for i in factors:
if i in prime_dict:
prime_dict[i] += 1
elif not (i in prime_dict):
prime_dict[i] =1 # create a dictionary of each prime factor
prime_word = '' # create a string for final output
for a, b in sorted(prime_dict.items()):
if int(b)==1:
prime_word += '({})'.format(a)
else:
prime_word += '({}**{})'.format(a,b)
return prime_word