Prime Factor | Python program to print prime factors ; A function to print all prime factors of a given number n ; Print the number of two 's that divide n ; n must be odd at this point so a skip of 2 ( i = i + 2 ) can be used ; while i divides n , print i ad divide n ; Condition if n is a prime number greater than 2 ; Driver Program to test above function
import math NEW_LINE def primeFactors ( n ) : NEW_LINE INDENT while n % 2 == 0 : NEW_LINE INDENT print 2 , NEW_LINE n = n / 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT print i , NEW_LINE n = n / i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT print n NEW_LINE DEDENT DEDENT n = 315 NEW_LINE primeFactors ( n ) NEW_LINE