-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.cpp
32 lines (27 loc) · 936 Bytes
/
3.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
/*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*/
#include <iostream>
int main() {
long number = 600851475143;
int biggestFactor;
while (number > 1) {
// This if lets us to use a step size of 2 for the loop
if (number % 2 == 0) {
biggestFactor = std::max(2, biggestFactor);
number = number / 2;
continue;
}
for (int i = 3; /* will break */ ; i += 2) {
if (number % i == 0) {
biggestFactor = std::max(i, biggestFactor);
number = number / i;
break;
}
}
}
std::cout << biggestFactor << std::endl;
return 0;
}