Smallest divisible number | Practice | GeeksforGeeks

Given a number N, find an integer denoting the smallest number evenly divisible by each number from 1 to n.

Example 1:
Input:
N = 3
Output: 6
Explanation: 6 is the smallest number 
divisible by 1,2,3.

Example 2:
Input:
N = 6
Output: 60
Explanation: 60 is the smallest number 
divisible by 1,2,3,4,5,6.

Your Task:  
You dont need to read input or print anything. Complete the function getSmallestDivNum() which takes N as input parameter and returns the smallest number evenly divisible by each number from 1 to n.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 25
0
Kartik Khanna8 months ago
Kartik Khanna
long long gcd(long long a,long long b){        return(b==0)?a:gcd(b,a%b);    }    long long getSmallestDivNum(long long n){     long long res=1;      for(long long i=2;i<=n;i++){          res=(res*i)/gcd(res,i);      }      return res;    }
0
sneha sinha8 months ago
sneha sinha
import mathclass Solution:    def getSmallestDivNum(self, n):         # code here         a=1        for i in range(2,n+1):            a=(a*i)//math.gcd(a,i)        return a
0
Ashu10 months ago
Ashu
Easy c++ Solution|| C++
0
101_Sumit Patil10 months ago
101_Sumit Patil
CPP SolutionExecution Time : 0.01 seclong long gcd(long long int a, long long int b){  if (b == 0)    return a;  return gcd(b, a % b);}     long long getSmallestDivNum(long long n)    {        long res=1;        for(long i =2; i<=n; i++)        {           res = (res*i)/gcd(res,i);         }        return res;    }
0
3lit3coder10 months ago
3lit3coder
0
Vishal Kumar Mahto11 months ago
Vishal Kumar Mahto
Simple solution in javaclass Solution{public static long getSmallestDivNum(int n){
    long s=1;
    for(long i =2;i<=n;i++)    {        s = lcm(s,i);    }
    return s ;     }
public static long gcd(long a,long b){    if(b==0)    return a ;    else    return gcd(b,a%b);}
public static long lcm(long a,long b){    return (a*b)/gcd(a,b);}}
-2
Akansha11 months ago
Akansha
If you observe carefully the ans must be the LCM of the numbers 1 to n. To find LCM of numbers from 1 to n
0
Amar Prakash1 year ago
Amar Prakash
long long gcdfun (long long A, long long B)          {              if( A < B)              {                  swap(A, B);              }
              if( A % B == 0)              {                  return B;              }              else              {                  return gcdfun(B, A % B);              }          }
    long long getSmallestDivNum(long long n){
        long long x = 1;        for(long long i=2; i<=n; i++)        {            x = (x*i) /gcdfun(x,i);        }
        return x;        // code here    }
0
Kumar Tejaswi1 year ago
Kumar Tejaswi
//define the gcd function first and than...
long long getSmallestDivNum(long long N){        // code here        long long x=1;        for (long long i=1;i<=N;i++)        {            x=(((i*x))/gcd(i,x));        }return x;    }
0
Annanya Mathur1 year ago
Annanya Mathur
long long getSmallestDivNum(long long n){
      long long int a=1,i;        for(  i=2;i<=n;i++)        {            a=(i*a)/__gcd(a,i);
         }         return a;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.