-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfactorize.cpp
More file actions
45 lines (37 loc) · 725 Bytes
/
factorize.cpp
File metadata and controls
45 lines (37 loc) · 725 Bytes
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
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/lambda/lambda.hpp>
using namespace std;
template <typename IntegerType, typename OutputIterator>
void prime_factor(IntegerType m, OutputIterator result)
{
while (m % 2 == 0)
{
m /= 2;
*result++ = 2;
}
if (m > 1)
{
for (IntegerType d = 3; m >= d * d; d += 2)
while ((m % d) == 0)
{
m /= d;
*result++ = d;
}
if (m > 1) *result++ = m;
}
}
int main()
{
using namespace boost::lambda;
int m;
while (std::cin >> m)
{
std::vector<int> v;
prime_factor(m, back_inserter(v));
for_each(v.begin(), v.end(), cout << _1 << ' ');
cout.put('\n');
}
return 0;
}