Smallest number divisible by first n numbers
PROBLEM :
Given a number n the task is to complete the function which returns an integer denoting the smallest number evenly divisible by each number from 1 to n.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each line contains an integer N.
Output:
For each test case output will be in new line denoting the smallest number evenly divisible by each number from 1 to n.
Constraints:
1<=T<=50
1<=n<=25
Example(To be used only for expected output):
Input:
2
3
6
Output:
6
60
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*You are required to complete this method */
long long lcm(long long ,long long ) ;
long long gcd(long long ,long long ) ;
long long getSmallestDivNum(long long no)
{
int i ;
long long ans=1 ;
for(i=1;i<=no;i++)
{
ans=lcm(ans,i) ;
}
return ans ;
}
long long lcm(long long a,long long b)
{
return a*b/(a>b?gcd(b,a):gcd(a,b)) ;
}
long long gcd(long long a,long long b)
{
long long rem ;
rem=b%a ;
if(rem==0)
return a ;
else
return gcd(rem,a) ;
}
---------------------------------------------------------------------------------
Given a number n the task is to complete the function which returns an integer denoting the smallest number evenly divisible by each number from 1 to n.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each line contains an integer N.
Output:
For each test case output will be in new line denoting the smallest number evenly divisible by each number from 1 to n.
Constraints:
1<=T<=50
1<=n<=25
Example(To be used only for expected output):
Input:
2
3
6
Output:
6
60
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*You are required to complete this method */
long long lcm(long long ,long long ) ;
long long gcd(long long ,long long ) ;
long long getSmallestDivNum(long long no)
{
int i ;
long long ans=1 ;
for(i=1;i<=no;i++)
{
ans=lcm(ans,i) ;
}
return ans ;
}
long long lcm(long long a,long long b)
{
return a*b/(a>b?gcd(b,a):gcd(a,b)) ;
}
long long gcd(long long a,long long b)
{
long long rem ;
rem=b%a ;
if(rem==0)
return a ;
else
return gcd(rem,a) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment