The below program is to check whether a number (say "n") prime factors are limited to 2,3,and 5 only or not. But program gives me signed integer overflow runtime error. Can anyone please help me in solving this problem? Here's the piece of code:
bool check(int n)
{
if(n<0)
n=n*(-1); // If 'n' is negative, making it positive.
int count=1; //'count' variable to check whether the number is divisible by 2 or 3 or 5.
while(n!=1 && count)
{
count=0;
if(n%2==0)
{
n/=2; count++;
}
else if(n%3==0)
{
n/=3; count++;
}
else if(n%5==0)
{
n/=5; count++;
}
}
if(n==1)
return true;
else return false;
}
Program gave me this error:
runtime error: signed integer overflow: -2147483648 * -1 cannot be represented in type 'int'
int64_tinstead of a 32 bitintlongorlong longMAX_INTif int is 32bit equals to2^31 -1 = 2147483647