Prime Factors Calculator


import math
list_factors =[]
def primefactors(n):
   list_2=[]
   list_f=[]
   while n % 2 == 0:
      list_2.append(0)
      n = n / 2 #loop to eliminate all powers of 2
   list_factors.append([2,len(list_2)])
 
    
   for i in range(3,int(math.sqrt(n))+1,2): #range(a,b,c) : a= lower limit , b= upper limit(not included), c= increment ,i.e., i = {3,5,7,9,11,13,15,...}
    while (n % i == 0): #powers of 2 have already been removed from n
        list_f.append(i) # as 3|9, before trying 9 the code tries 3 and reduces n thus eliminating the chance for it to be divisible by 9 or higher powers
        n = n / i
    def exp(i):
    	return list_f.count(i)
    list_factors.append([i,list_f.count(i)])
    
    
   if n > 2: #if even after reducing by primes upto sqrt(n) there remains a number then it is also prime eg: 790 = 2×5×79 , but int(sqrt(790)) = 28<79
      list_factors.append([n,1])
   return list_factors
 
n = int(input('n: '))
result = primefactors(n)
print(n ,'= 1',end=" ")
step = 0
while step < len(result):
	def base(step):
		return result[step][0]
	def expnt(step):
		return result[step][1]
	if expnt(step) != 0:
		print('×','(',base(step),'^',expnt(step),')',end=" ")
	step = step +1
    
    

Comments