0

I am getting a problem with following code that "Floating point exception, core dumped" but I have not even one float or double variable. By using checking printf, i observed that it happens in isPrimeFunction, where there execution jams.

/*
PROBLEM 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/


#include <stdio.h>

typedef int bool;
const bool true=1;
const bool false=0;

bool isPrime(long long int number) {
    long long int i;
    for(i=2; i < (number/2); i++) {
        if(number%i == 0) {
            return false;
        }
    }
    return true;
}   

int main() {
    long long int number, largest;
    number=600851475143;
    largest=0;
    int i;

    for(i=1; i <= (number/2); i++) {
        if((number % i) == 0) {
            if(isPrime(i) == true) {
                largest=i;
            }
        }
    }
    if(largest != 0)    
        printf("Largest prime factor is: %lli\n", largest);
    else
        printf("There is no prime factor of the number.\n");    

    return 0;
}
Fabio_MO
  • 713
  • 1
  • 13
  • 26
  • 1
    Unrelated but don't you need an L after your long long value? – Dave Newton Mar 15 '18 at 14:39
  • 2
    Does't i in your main() should be a long long int and not an int? – Fabio_MO Mar 15 '18 at 14:42
  • actually it is for input from user but for testing sake I put that 12 digit number. If I put the first number, the result is exactly same as there is 29. – Ahmad Raza Mar 15 '18 at 14:45
  • 2
    Perhaps not related, but your code is horribly inefficient. Even if it works perfectly, it would take many years to produce an output. The loop in `main()` iterates 300 billion times, and the loop in `isPrime()` iterates, on average, 150 billion times. Correct me if I'm wrong, but isn't that 4.5 quintillion iterations before your program finishes? – r3mainer Mar 15 '18 at 14:47
  • Sister Fister, GCC COMPILER 5.4 && Core 2 du inside && 64 bit OS – Ahmad Raza Mar 15 '18 at 14:47
  • Not related to an exception, but with extremely little effort the loop could be speeded up by some magnitudes.... Why testing for each even number? Why testing for prime numbers? You could simply remove every factor you find from the number by dividing it (also multiple occurances of same factor). You cannot find any non prime factors afterwards. – Gerhardh Mar 15 '18 at 16:19
  • The bool, true, false are all properly defined in the header file: `stdbool.h` Strongly suggest using that header file rather than trying to re-create the names and values. – user3629249 Mar 15 '18 at 18:03
  • I made the suggested corrections to the posted code. Now it does not have a 'floating point exception' but it has been totally consuming one of my CPU cores and has been running for 30 minutes with no sign of ever completing. – user3629249 Mar 15 '18 at 18:07
  • I know this is very hard taking task, but I had to do this because of questions at https://projecteuler.net/ , so I need corrections but I'de to run it. It took 15 minutes to mine pc, with cpu usage 50%. – Ahmad Raza Mar 16 '18 at 09:40

1 Answers1

6

In main your i is an int and therefore most likely not big enough to fit number/2 which means that it's quite possible (although this is undefined behavior) that i will wrap and eventually end up being 0. number % i will then cause division by zero which is another undefined behavior but quite likely why your system generates a floating point exception (mine does).

N.B. This is not a very good algorithm. Just count how many times you'll loop. With a number like 600 billion that you have even if it's prime and you never trigger the isPrime test inside the loop you're looking at runtimes close to an hour. Every time you hit the test inside the loop you're increasing your runtime by a lot.

Art
  • 19,807
  • 1
  • 34
  • 60