I need to find the largest prime factor of the number 600851475143.
Because it is a very large number, I can not store it in a variable of the normal integer type.
The code I made to find the largest prime factor of a number is this:
#include <iostream>
using namespace std;
int main()
{
int maiorFator = 0;
unsigned long int numero = 600851475143;
for (int i = 2; numero != 1;) {
if (numero % i == 0) {
numero /= i;
if (maiorFator < i) {
maiorFator = i;
}
cout << i << endl;
}
else {
i++;
}
}
cout << "\nMaior fator primo: " << maiorFator << endl;
return 0;
}
I've already tried to use the long
and unsigned
f modifiers in the numero
variable, but it does not work, there is always overflow , and because of that I can not get the result.
I can not use variables of type float
nor double
, because I use the rest %
operator to do the calculations.
The factorization algorithm works well, at least for the numbers I tested. My only problem is not being able to store the large number.