So i solved the problem and was looking through other solutions and found this:
n = 600851475143
i = 2
while i*i < n:
while n%i == 0:
n = n / i
i = i + 1
print ('answer is', n)
I don't really get why this solution works. Can anyone explain this to me?
BTW: This is my solution in c++:
#include <iostream>
#include <dinput.h>
#include <vector>
using namespace std;
long long largestpfactor(long long num) {
double start = sqrt(num);
for(int i = start; i > 1; i--) {
if((num % i) == 0) {
long long div = 2;
while(((i % div) != 0) && div < i) {
div++;
}
if(div == i) return i;
}
}
return NULL;
}
int main() {
cout << largestpfactor(600851475143) << endl;
while(!GetAsyncKeyState(VK_END)) {
}
}