LCM of First n Natural Numbers | to calculate hcf ; to calculate lcm ; lcm ( a , b ) = ( a * b ) / hcf ( a , b ) ; assign a = lcm of n , n - 1 ; b = b - 1 ; base case ; Function call pass n , n - 1 in function to find LCM of first n natural number
function hcf ( a , b ) { if ( b == 0 ) return a ; return hcf ( b , a % b ) ; } function findlcm ( a , b ) { if ( b == 1 ) return a ; a = ( a * b ) / hcf ( a , b ) ; b -= 1 ; return findlcm ( a , b ) ; } document . write ( n ) ; else document . write ( findlcm ( n , n - 1 ) ) ;