Largest divisor of a number not divisible by another given number | Function to find the largest number X such that it divides P but is not divisible by Q ; Stores the frequency count of of all Prime Factors ; Increment the frequency of the current prime factor ; If Q is a prime factor ; Stores the desired result ; Iterate through all divisors of Q ; Stores the frequency count of current prime divisor on dividing P ; Count the frequency of the current prime factor ; If cur is less than frequency then P is the final result ; Iterate to get temporary answer ; Update current answer ; Print the desired result ; Given P and Q ; Function Call
function findTheGreatestX ( P , Q ) { var divisiors = new Map ( ) ; for ( var i = 2 ; i * i <= Q ; i ++ ) { while ( Q % i == 0 && Q > 1 ) { Q = parseInt ( Q / i ) ; if ( divisiors . has ( i ) ) divisiors . set ( i , divisiors . get ( i ) + 1 ) else divisiors . set ( i , 1 ) } } if ( Q > 1 ) if ( divisiors . has ( Q ) ) divisiors . set ( Q , divisiors . get ( Q ) + 1 ) else divisiors . set ( Q , 1 ) var ans = 0 ; divisiors . forEach ( ( value , key ) => { var frequency = value ; var temp = P ; var cur = 0 ; while ( temp % key == 0 ) { temp = parseInt ( temp / key ) ; cur ++ ; } if ( cur < frequency ) { ans = P ; } temp = P ; for ( var j = cur ; j >= frequency ; j -- ) { temp = parseInt ( temp / key ) ; } ans = Math . max ( temp , ans ) ; } ) ; document . write ( ans ) ; } var P = 10 , Q = 4 ; findTheGreatestX ( P , Q ) ;