-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimeFactorizer.py
35 lines (31 loc) · 1.07 KB
/
primeFactorizer.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
def primeFactorizer(number):
# number = int(input("What number would you like to prime factorize? "))
if number <= 0:
print("Number cannot be prime factorized.")
exit()
if number == 1:
print(1)
currentPrime = 2
primeList = []
while number > 1:
temp = number/currentPrime
if temp % 1 == 0:
number /= currentPrime
primeList.append(currentPrime)
else:
currentPrime += 1
uniqueFactors = list(set(primeList))
uniqueFactors.sort()
powers = []
for i in range(len(uniqueFactors)):
power = primeList.count(uniqueFactors[i])
powers.append(power)
for i in range(len(uniqueFactors)):
power = primeList.count(uniqueFactors[i])
if power != 1 and i != len(uniqueFactors)-1:
print(f'{uniqueFactors[i]}^{power} * ', end="")
elif i != len(uniqueFactors)-1:
print(f'{uniqueFactors[i]} * ', end="")
else:
print(f'{uniqueFactors[i]}', end="")
primeFactorizer(40)