What is Prime Factorization?
Every natural number can be expressed as a product of prime numbers. Such a product representation is called prime factorization.
For Example:
300 = 2 ^ 2 * 3 ^ 1 * 5 ^ 2
580 = 2 ^ 2 * 5 ^ 1 * 29 ^ 1
Write a program to display the prime factorization of a given number in Python.
Code:
from math import sqrt
a=int(input())
t,b,i=[],a,3
while not(a%2):
t.append(2)
a//=2
while i<=a:
if a%i == 0:
a//=i
t.append(i)
else:
i+=2
if 1 in t: t.remove(1)
print('{} = '.format(b),end='')
print(*['{}{}'.format(p,'^'+str(t.count(p)) if t.count(p)>1 else '') for p in sorted(set(t))],sep=' x ')
Output
186 = 2 x 3 x 31