Python Program to Find Prime Factors of a Number

(652 Views)


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

Prime Factorization Program Implementation in Python
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

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search

Play 2048 Game Online

Search Tags

    Find Prime factorization using Python

    Efficient Program to Print all prime factors of a given number

    Compute Prime Factors of an integer in Python

    Python Program to Compute Prime Factors of an Integer