Skip to content
← Back to Community
Prime Factorization in Python
Profile icon
EmmaGao8

How do you make Python get the prime factorization of a number the fastest easiest way? I am not fimiliar with the math module so I didn't make code yet.

Answered by codebroken [earned 5 cycles]
View Answer
Voters
Profile icon
codebroken
Profile icon
EmmaGao8
Comments
hotnewtop
Profile icon
codebroken

Example:

import math # Below function will print the # all prime factor of given number def prime_factors(num): # Using the while loop, we will print the number of two's that divide n while num % 2 == 0: print(2,) num = num / 2 for i in range(3, int(math.sqrt(num)) + 1, 2): # while i divides n , print i ad divide n while num % i == 0: print(i,) num = num / i if num > 2: print(num) # calling function num = 200 prime_factors(num)

Output:

2 2 2 5 5

You could check this article, might help you!

Profile icon
JBloves27

Hiya, you could look at this for more info!

Hope this helps!

Profile icon
EmmaGao8

@JBloves27 I want the prime FACTORIZATION, not prime numbers.

Profile icon
JBloves27