Prime Factor | A function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver code
function primeFactors ( n ) { while ( n % 2 == 0 ) { document . write ( 2 + " " ) ; n = Math . floor ( n / 2 ) ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { document . write ( i + " " ) ; n = Math . floor ( n / i ) ; } } if ( n > 2 ) document . write ( n + " " ) ; } let n = 315 ; primeFactors ( n ) ;