Product of factors of number | Javascript program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; driver function
let M = 1000000007 ; function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } function countFactors ( n ) { let count = 0 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } function multiplyFactors ( n ) { let numFactor = countFactors ( n ) ; let product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * Math . sqrt ( n ) ) % M ; return product ; } let n = 12 ; document . write ( multiplyFactors ( n ) ) ;