C - determine if a number is prime
Solution 1:
OK, so forget about C. Suppose I give you a number and ask you to determine if it's prime. How do you do it? Write down the steps clearly, then worry about translating them into code.
Once you have the algorithm determined, it will be much easier for you to figure out how to write a program, and for others to help you with it.
edit: Here's the C# code you posted:
static bool IsPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0 && i != number) return false;
}
return true;
}
This is very nearly valid C as is; there's no bool
type in C, and no true
or false
, so you need to modify it a little bit (edit: Kristopher Johnson correctly points out that C99 added the stdbool.h header). Since some people don't have access to a C99 environment (but you should use one!), let's make that very minor change:
int IsPrime(int number) {
int i;
for (i=2; i<number; i++) {
if (number % i == 0 && i != number) return 0;
}
return 1;
}
This is a perfectly valid C program that does what you want. We can improve it a little bit without too much effort. First, note that i
is always less than number
, so the check that i != number
always succeeds; we can get rid of it.
Also, you don't actually need to try divisors all the way up to number - 1
; you can stop checking when you reach sqrt(number). Since sqrt
is a floating-point operation and that brings a whole pile of subtleties, we won't actually compute sqrt(number)
. Instead, we can just check that i*i <= number
:
int IsPrime(int number) {
int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) return 0;
}
return 1;
}
One last thing, though; there was a small bug in your original algorithm! If number
is negative, or zero, or one, this function will claim that the number is prime. You likely want to handle that properly, and you may want to make number
be unsigned, since you're more likely to care about positive values only:
int IsPrime(unsigned int number) {
if (number <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) return 0;
}
return 1;
}
This definitely isn't the fastest way to check if a number is prime, but it works, and it's pretty straightforward. We barely had to modify your code at all!
Solution 2:
I'm suprised that no one mentioned this.
Use the Sieve Of Eratosthenes
Details:
- Basically nonprime numbers are divisible by another number besides 1 and themselves
- Therefore: a nonprime number will be a product of prime numbers.
The sieve of Eratosthenes finds a prime number and stores it. When a new number is checked for primeness all of the previous primes are checked against the know prime list.
Reasons:
- This algorithm/problem is known as "Embarrassingly Parallel"
- It creates a collection of prime numbers
- Its an example of a dynamic programming problem
- Its quick!
Solution 3:
Stephen Canon answered it very well!
But
- The algorithm can be improved further by observing that all primes are of the form 6k ± 1, with the exception of 2 and 3.
- This is because all integers can be expressed as (6k + i) for some integer k and for i = −1, 0, 1, 2, 3, or 4; 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides (6k + 3).
- So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6k ± 1 ≤ √n.
-
This is 3 times as fast as testing all m up to √n.
int IsPrime(unsigned int number) { if (number <= 3 && number > 1) return 1; // as 2 and 3 are prime else if (number%2==0 || number%3==0) return 0; // check if number is divisible by 2 or 3 else { unsigned int i; for (i=5; i*i<=number; i+=6) { if (number % i == 0 || number%(i + 2) == 0) return 0; } return 1; } }
Solution 4:
- Build a table of small primes, and check if they divide your input number.
- If the number survived to 1, try pseudo primality tests with increasing basis. See Miller-Rabin primality test for example.
- If your number survived to 2, you can conclude it is prime if it is below some well known bounds. Otherwise your answer will only be "probably prime". You will find some values for these bounds in the wiki page.