1
I need to find the largest prime factor of the number 600851475143.
Because it is a very large number, I cannot store it in a normal integer type variable.
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 tried using the modifiers long
and unsigned
in the variable numero
, but it doesn’t work, it always occurs overflow, and because of that I can’t get the result.
I can’t use variables like float
nor double
, because I use the rest operator %
to make 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.
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero