Find the first natural number whose factorial is divisible by x | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial divides x . ; let i = 1 ; Result ; Remove common factors ; We found first i . ; Driver code
function gcd ( a , b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } function firstFactorialDivisibleNumber ( x ) { let new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x = parseInt ( new_x / gcd ( i , new_x ) , 10 ) ; if ( new_x == 1 ) break ; } return i ; } let x = 16 ; document . write ( firstFactorialDivisibleNumber ( x ) ) ;