Number of N length sequences whose product is M | Function to calculate the value of ncr effectively ; Initializing the result ; Multiply and divide simultaneously to avoid overflow ; Return the answer ; Function to return the number of sequences of length N such that their product is M ; Hashmap to store the prime factors of M ; Calculate the prime factors of M ; If i divides M it means it is a factor Divide M by i till it could be divided to store the exponent ; Increase the exponent count ; If the number is a prime number greater than sqrt ( M ) ; Initializing the ans ; Multiply the answer for every prime factor ; entry . getValue ( ) represents the exponent of every prime factor ; Return the result ; Driver code
function nCr ( n , r ) { let res = 1 ; for ( let i = 1 ; i <= r ; i ++ ) { res *= ( n - r + i ) ; res = Math . floor ( res / i ) ; } return res ; } function NoofSequences ( N , M ) { let prime = new Map ( ) ; for ( let i = 2 ; i <= Math . sqrt ( M ) ; i ++ ) { while ( M % i == 0 ) { if ( prime . get ( i ) == null ) prime . set ( i , 1 ) ; else { let x = prime . get ( i ) ; prime . set ( i , ++ x ) ; } M = Math . floor ( M / i ) ; } } if ( M > 1 ) { if ( prime . get ( M ) != null ) { let x = prime . get ( M ) ; prime . set ( M , ++ x ) ; } else prime . set ( M , 1 ) ; } let ans = 1 ; for ( let [ key , value ] of prime . entries ( ) ) { ans *= ( nCr ( N + value - 1 , N - 1 ) ) ; } return ans ; } let N = 2 , M = 6 ; document . write ( NoofSequences ( N , M ) ) ;