-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem3.cpp
46 lines (38 loc) · 1.1 KB
/
problem3.cpp
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
#include <iostream>
#include <list>
#include <cmath>
using namespace std;
list<unsigned long long> factorize(unsigned long long value) {
list<unsigned long long> out;
if(value <= 3) {
return out;
}
for(unsigned long long i = sqrt(value); i > 1; i--)
if(value % i == 0) // fattore
out.push_back(i);
bool modified = true;
while(modified) {
modified = false;
for(list<unsigned long long>::iterator it=out.begin(); it != out.end(); ++it) {
list<unsigned long long> t = factorize(*it);
if (!t.empty()) {
out.remove(*it);
out.merge(t);
out.sort();
out.unique();
modified = true;
break;
}
}
}
return out;
}
int main() {
list<unsigned long long> fact = factorize(600851475143);
unsigned long long max = 0;
for(list<unsigned long long>::iterator it=fact.begin(); it != fact.end(); ++it)
if(*it > max)
max = *it;
cout << "Problem 3 solution " << max << '\n';
return 0;
}