Python Program to Find Prime Factors of a Number

[5826 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
        




Getting any Errors while coding? Ask us on our new Forum:




Liked Article? Please Buy Author a Coffee


Comments



Search
For Sponsored Advertisement OR Freelance Programming Article Submission, Contact us at atechdailyweb@gmail.com
Recommended Deal Ends in











Quizzes:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Why does everyone hate Java?
How much JavaScript to learn for Web Development
How to Convert EPOCH to Date in Java
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