-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem003.cpp
More file actions
40 lines (38 loc) · 817 Bytes
/
problem003.cpp
File metadata and controls
40 lines (38 loc) · 817 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
#include <cmath>
#include <iostream>
long long largestPrimeFactor(long long n)
{
if (n < 2) {
return -1;
}
int a[] = { 2, 3, 5 };
long long x = n;
for (int i = 0; i < 3; ++i) {
int factor = a[i];
while (x % factor == 0) {
x /= factor;
}
if (x == 1) {
return factor;
}
}
int inc[] = { 4, 2, 4, 2, 4, 6, 2, 6 };
int idx = 0;
long long factor = 7;
long long sqrtX = sqrt(x);
while (factor <= sqrtX) {
if (x % factor == 0) {
x /= factor;
sqrtX = sqrt(x);
} else {
factor += inc[idx];
idx = (idx + 1) % 8;
}
}
return x;
}
int main()
{
std::cout << "Answer: " << largestPrimeFactor(600851475143) << '\n';
return 0;
}