HACKER RANK Project Euler 05 - Smallest Multiple - Solution.'C++'
Project Euler Challenges-05 - Smallest Multiple Solution
Problem Statement
This problem is a programming version of Problem 5 from projecteuler.net
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N ?
Input Format
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N .
Output Format
Print the required answer for each test case.
Constraints
1≤T≤10
1≤N≤40
Sample Input
2
3
10
Sample Output
6
2520
Problem Statement
This problem is a programming version of Problem 5 from projecteuler.net
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from
Input Format
First line containsT that denotes the number of test cases. This is followed by T lines, each containing an integer, N .
First line contains
Output Format
Print the required answer for each test case.
Print the required answer for each test case.
Constraints
1≤T≤10
1≤N≤40
Sample Input
2
3
10
Sample Output
6
2520
Solution:
#include <iostream>
using namespace std;
int a[50][50];
int main()
{
for(int i=2;i<=40;++i)
{
int temp=i;
int j=2;
while(temp!=1)
{
while(temp%j==0)
{
temp/=j;
++a[i][j];
}
++j;
}
}
int t;
cin>>t;
for(int k=1;k<=t;++k)
{
int n;
cin>>n;
long long ans=1;
for(int i=2;i<=n;++i)
{
int mmax=a[2][i];
for(int j=2;j<=n;++j)
mmax=max(mmax,a[j][i]);
for(int k=1;k<=mmax;++k)
ans*=i;
}
cout<<ans<<endl;
}
}
#include <iostream>
using namespace std;
int a[50][50];
int main()
{
for(int i=2;i<=40;++i)
{
int temp=i;
int j=2;
while(temp!=1)
{
while(temp%j==0)
{
temp/=j;
++a[i][j];
}
++j;
}
}
int t;
cin>>t;
for(int k=1;k<=t;++k)
{
int n;
cin>>n;
long long ans=1;
for(int i=2;i<=n;++i)
{
int mmax=a[2][i];
for(int j=2;j<=n;++j)
mmax=max(mmax,a[j][i]);
for(int k=1;k<=mmax;++k)
ans*=i;
}
cout<<ans<<endl;
}
}
Comments
Post a Comment