I have no idea why g++ doesn't like my code. It ran fine in java. Any insights would be greatly appreciated.
#include<iostream>
using namespace std;
bool isPrime(long number);
int main(){
const long number = 600851475143;
long max = 0;
for(long i= 0; i*i <= number; i++)
if(number % i == 0 && isPrime(i))
max = i;
cout<< max << endl;
return 0;
}
bool isPrime(long number){
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(long i= 3; i*i <= number; i+=2)
if(number % i == 0)
return false;
return true;
}
A floating point exception is an error that occurs when you try to do something impossible with a floating point number, such as divide by zero. In fluent floating point error can be caused by many factors such as, improper mesh size, defining some property close to zero.
Floating-point exceptions in VFPThe exception is caused if the result of an operation has no mathematical value or cannot be represented. Division by zero. The exception is caused if a divide operation has a zero divisor and a dividend that is not zero, an infinity or a NaN. Overflow.
const long number = 600851475143;
There is overflow, long can't hold that big number.
see this link
LONG_MAX is 2147483647
try:
const unsigned long long number = 600851475143;
unsigned long longmax = 0;
Edit:
You can't % against 0, i
starts from 0
for(long i= 0; i*i <= number; i++)
^^
{
if(number % i == 0 && isPrime(i))
^^^
{
max = i;
cout<< max << endl;
}
}
Minor change to a working version:
bool isPrime(unsigned long long number);
int main(){
const unsigned long long number = 600851475143;
unsigned long long max = 0;
for(long i = 1; i*i <= number; i++)
{
if(number % i == 0 && isPrime(i))
{
max = i;
cout<< max << endl;
}
}
return 0;
}
bool isPrime(unsigned long long number)
{
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(unsigned long long i= 3; i*i <= number; i+=2)
{
if(number % i == 0)
{
return false;
}
}
return true;
}
I don't see a floating point anywhere, but if I had to guess it's because it's due to overflow. Use unsigned long long
or long long
instead of regular long
.
sizeof(long)
on some compilers has evaluated to 4, similar to sizeof(int)
, which means that the limit of long
is 2147483647. long long
is required by the C++ standard to be at least 64-bits, double that of long
and int
, which has a signed maximum of 9223372036854775807.
The error stems from your code: You're doing modulus by zero, which is wrong.
Consider doing this instead:
#include <iostream>
using namespace std;
bool isPrime(unsigned long long number);
int main(){
const unsigned long long number = 600851475143;
unsigned long long max = 0;
for(unsigned long long i= 1; i*i <= number; i++)
if(number % i == 0 && isPrime(i))
max = i;
cout<< max << endl;
return 0;
}
bool isPrime(unsigned long long number) {
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(unsigned long long i= 3; i*i <= number; i+=2)
if(number % i == 0)
return false;
return true;
}
Notice how i = 0
was changed to i = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With