Smallest number divisible by first n numbers | C ++ program to find smallest number evenly divisible by all numbers 1 to n ; Function returns the lcm of first n numbers ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long lcm ( long long n ) { long long ans = 1 ; for ( long long i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( __gcd ( ans , i ) ) ; return ans ; } int main ( ) { long long n = 20 ; cout << lcm ( n ) ; return 0 ; }