Reputation: 23
I have here a simple algorithm for factorizing.
void primeFactor(int number){
if (number == 1)return;
int x = 2;
while (number%x != 0)x++;
cout << x << endl;
primeFactor(number / x);
}
It works fine for small numbers, but when ever I enter a big number like 809800987876, I get a -1 after about 3 factors.
So here's sample output for 809800987876.
> 2 2 486957767
> -1
How can I fix this?
Upvotes: 2
Views: 194
Reputation: 180415
You are overflowing the int
. On a typical system the maximum value of an int
is 2147483647. 809800987876 is larger than that so it overflows. You can use a long long
which has at least a max of 9223372036854775807.
Upvotes: 2