-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.cpp
More file actions
64 lines (55 loc) · 1.12 KB
/
5.cpp
File metadata and controls
64 lines (55 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
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 by all of the numbers from 1 to 20?
*/
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
bool is_prime(int);
int main()
{
vector<int> factor;
for (int i = 2; i < 21; ++i)
{
if (is_prime(i))
factor.push_back(i);
else
{
vector<int> x = factor;
int temp = i;
while(x.size() > 0)
{
if (temp % x.back() == 0)
temp = temp/x.back();
x.pop_back();
}
if (temp != 1)
factor.push_back(temp);
}
}
//cout << "Size = " << factor.size() << endl;
int result = 1;
while (factor.size())
{
result = result * factor.back();
cout << "factor= " << factor.back() << endl;
factor.pop_back();
}
cout << "Result= " << result << endl;
}
bool is_prime(int n)
{
int i,sq,count=0;
if(n==1 || n==2)
return true;
if(n%2==0)
return false;
sq=sqrt(n);
for(i=2;i<=sq;i++)
{
if(n%i==0)
return false;
}
return true;
}