Smallest number divisible by first n numbers | Function for gcd ; Function returns the lcm of first n numbers ; function call
function gcd ( a , b ) { if ( a % b != 0 ) return gcd ( b , a % b ) ; else return b ; } function lcm ( n ) { let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( gcd ( ans , i ) ) ; return ans ; } let n = 20 ; document . write ( lcm ( n ) ) ;