You are not logged in.
I've tried to create a simple function to determine if a number is prime or not.. whenever I compile and try to run I get a floating point exception and can't quite figure out why, I'm very new to C and compiling manually using gcc so any help is appreciated.
here's my code: http://pastebin.com/m2f19dbac
Last edited by zandaa (2008-10-01 06:36:35)
With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.
Offline
Your problem is in isprime, you should be initializing i to be 1 instead of 0. Otherwise, you're dividing by zero, which isn't cool.
Offline
I found the problem with initializing i=0 instead of i=1, but it gives me numbers that aren't even prime numbers, so where am I going wrong with this? what's wrong with my logic?
With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.
Offline
you should probably uncomment that first 'break' statement.
Offline
I figured something out and yet have no idea why it worked, I have turned around my whole if statements into something like:
if ( (x%i == 0 && i != 1) && (x%i == 0 && i != x) ) {
result = 0;
break;
} else if (x%i == 0 && i == x) {
result = 1;
//printf("%d\n", x);
break;
}
With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.
Offline
If you start the for loop counting from 2 there is no need to keep tesing for i = 1, theres no need for i to be 1 anyway. You could also replace the break statements with 'return result' since the only thing you do after the for loop is to return result anyway.
EDIT: scratch what I wrote above, theres a better way.
A number is prime if it can only divide by itself and 1, if you change the isprime routine to
int isprime(int x) {
int i, result;
result = 1;
for (i=2; i < x; i++) {
if ( x % i == 0)
{
result = 0;
return result;
}
}
return result;
}
We initialise result to be 1 and then loop i between 2 and x-1, if x % i is ever zero then we know that x is not prime so we set result to 0 and return, if x % i is never 0 then result is still 1 and x must be prime so we can return result.
Last edited by ghostHack (2008-09-25 21:32:23)
Offline
I'm quite useless at programming:
for (i=0; i <= x; i++) {
if (x%i == 0 && i != 1) {
result = 0;
//break;
}
if (i == x) {
result = 1;
//printf("%d\n", x);
break;
}
}
This loop always ends with result=1 because the last loop has i=x and therefore it changes the result value to 1.
There are also many checks you could avoid with a "smart" for cycle:
for (i=2; i <= x; i++) {
if (x%i == 0) {
return 0;
}
}
return 1
I was able to remove the "i != 1" part by having the for loop starting from 2.
I removed all the if (i == x) clause because that only happens at the end of the for cycle.
Also you could be smart and eliminate some values from the loop: for example you could avoid checking all the numbers higher than n/2 or than sqrt(n), but that's more related to the mathematics behind than to the programming
Hope this helps
edit: beaten
Last edited by carlocci (2008-09-25 21:36:04)
Offline
Hmm, I see you're trying to find all primes between 2 and 10000. You could use Erastotenes sieve. Divide only by values that you know that are prime. A little example: Checking if 169 is prime: 169%2 != 0, 169%3 != 0, 169%5 != 0, 169%7 != 0, 169%11 != 0, 169%13 == 0, so 169 is not a prime. The idea, only to check numbers lower then or equal sqrt(x) applies here to.
"The bureaucracy is expanding to meet the needs of an expanding bureaucracy"
Offline