76

I am trying to come up with a method that takes an integer and returns a boolean to say if the number is prime or not and I don't know much C; would anyone care to give me some pointers?

Basically, I would do this in C# like this:

static bool IsPrime(int number)
{
    for (int i = 2; i < number; i++)
    {
        if (number % i == 0 && i != number)
            return false;
    }
    return true;
}
Totem
  • 7,189
  • 5
  • 39
  • 66
Jimmy
  • 9,686
  • 14
  • 59
  • 78
  • 51
    Here's some pointers: int *ptr; int *ptr2; int *ptr3. Sorry couldn't help it. How big are the numbers you will be checking? And also, do you want a heuristic or something that always works? – AlbertoPL Oct 08 '09 at 15:47
  • 12
    What's the point of 'i != number' when you have 'i < number' as a condition to execute the loop? – Matthieu M. Oct 08 '09 at 17:36
  • Take a look at this link this contains an explanation of how things work. http://cprogramming.language-tutorial.com/2012/01/check-whether-given-no-is-prime-or-not.html –  Mar 03 '12 at 17:27
  • 3
    Also note that checking `i < number` is overkill. By definition, if a number `x = a * b`, either `a` or `b` is `< int(sqrt(x))` and the other is greater. So your loop should only need to go up to `int(sqrt(x))`. – twalberg May 17 '13 at 14:28

12 Answers12

154

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!

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 12
    FYI, the C99 standard defines a header that provides `bool`, `true`, and `false`. – Kristopher Johnson Oct 08 '09 at 16:38
  • also forgot to mention that i don't need to worry about 0, 1, or negatives, this is just running through a list of 50 numbers if i remember correctly but thank you again – Jimmy Oct 08 '09 at 17:16
  • @Kristopher: thanks, forgot about that one. @Jimmy: happy to help, glad that it was only a documentation bug =) – Stephen Canon Oct 08 '09 at 17:19
  • You also can improve this (by nearly 50%) by only checking the odd numbers (treat 0,1,2 as special cases) and start at 3 in advance by 2 each time. – Tom Oct 08 '09 at 17:37
  • 29
    I know that it is simpler to compute a square than a square root, however computing a square on each iteration ought to cost MORE that computing the square root once and be done with it :x – Matthieu M. Oct 08 '09 at 17:39
  • 6
    On a modern out-of-order machine, the latency of the mul instruction to square i should be entirely hidden in the latency of the modulus, so there would be no appreciable performance win. On a strictly in-order machine, there is a win to be had using a hoisted square root, but that potentially raises issues of floating-point imprecision if the code were compiled on a platform with a large int type (64 bits or bigger). All that can be dealt with, but I thought it best to keep things simple and trivially portable. After all, if you care about speed, you're not using this algorithm at all. – Stephen Canon Oct 08 '09 at 17:51
  • 5
    @Tom you can improve a lot more by stopping at the floor(sqrt(number)). Take 11, for example, floor(sqrt(11)) = 3. The number after 3 is 4, 3*4 = 12 > 11. If you're using a naive sieve to check for primality, you only need to check odd numbers up to the sqrt of the original, aside from 2. – Calyth Oct 08 '09 at 23:08
  • 1
    @Matthieu M.: Agreed. I wrote a ruby script to test this: 331: Stephen: 0.000635 Matthieu: 0.000611 68720001023: Stephen: 1.958449 Matthieu: 0.003167 999999000001: Stephen: 7.293912 Matthieu: 0.005871 – aarona May 30 '10 at 04:39
  • 1
    @Calyth adding floor() to the function doesn't do anything different then sqrt() by itself does but it takes longer to complete the task. – aarona May 30 '10 at 04:58
  • @StephenCanon: The for loop terminating condition is: "i < number" then how come, if (number % i == 0 && i != number) holds? I am referring to "i != number" – RajSanpui May 27 '13 at 04:46
  • 3
    -1. The final function gives the incorrect answer for [4294967291](https://www.wolframalpha.com/input/?i=is+4294967291+prime). – davidg Apr 07 '14 at 03:17
  • @StephenCanon: and you can also replace that post increment `i++` with `++i`. as it will also decrease one step of working. – Abdullah Jan 05 '15 at 17:40
  • @StephenCanon, I would use `i – Z boson Apr 29 '15 at 08:18
  • @Zboson: If we really want to nitpick, `number/i` is strictly worse than (pre-computed) `sqrt(number)`. `number/i` performs one additional unnecessary division. It also makes the loop condition depend on a long-latency operation; on an architecture with a pipelined divide, this could easily cause stalls due to limits on the number of unresolved branch predictions in flight at once. – Stephen Canon Apr 29 '15 at 10:41
  • @StephenCanon, you already do `number%i` so you get `number/i` for free. It's just as fast as `sqrt(number)`. Did you read my [conditional-tests-in-primality-by-trial-division](https://stackoverflow.com/questions/22556599/conditional-tests-in-primality-by-trial-division)? – Z boson Apr 29 '15 at 12:21
  • @Zboson: it's as fast *on an architecture without pipelined divide*, and it's no clearer (in my opinion, tastes may vary). – Stephen Canon Apr 29 '15 at 13:17
  • The code posted by the OP was already 100% valid C. So a large portion of this answer is just nonsense, there is no need to convert a single thing. `bool` `true` and `false` do exist in C. `static` functions exist in C and variable declarations inside for loops are allowed. – Lundin Oct 26 '15 at 12:56
  • u can also get rid of all the even numbers, so i+=2 – Kasparov92 Apr 04 '16 at 18:45
  • @Kasparov92 `i` starts from 2, so `i+=2` is not right. You can try to input 31 or 33 to test it. – CyberMew Apr 30 '16 at 19:08
  • 1
    @CyberMew let i start from 3 and i+=2 so that we move on all odd only – Kasparov92 May 01 '16 at 10:17
  • Being a highly voted answer, the corner errors deserve attention - _in code_. 1) `IsPrime(0 or 1)` incorrectly reports true. 2) Undefined behavior with `IsPrime(int number)` and large `number`, 3) `IsPrime(unsigned number)`: `i*i<=number` is a problem when number near `UINT_MAX` as `i*i` overflows. Border line infinite loop when `number == UINT_MAX`. – chux - Reinstate Monica Feb 10 '19 at 17:56
  • The second function returns `1` for `n = 1` and 1 is not a prime number. – Silidrone Nov 19 '21 at 07:00
29

I'm suprised that no one mentioned this.

Use the Sieve Of Eratosthenes

Details:

  1. Basically nonprime numbers are divisible by another number besides 1 and themselves
  2. 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:

  1. This algorithm/problem is known as "Embarrassingly Parallel"
  2. It creates a collection of prime numbers
  3. Its an example of a dynamic programming problem
  4. Its quick!
monksy
  • 14,156
  • 17
  • 75
  • 124
  • 9
    It's also `O(n)` in space, and as long as your computation is for a single value, this is a huge waste of space for no performance gain. – R.. GitHub STOP HELPING ICE May 29 '11 at 21:15
  • 3
    (Actually `O(n log n)` or larger if you're supporting large numbers...) – R.. GitHub STOP HELPING ICE May 29 '11 at 21:16
  • 2
    Who computes only 1 value for a prime for the life span of the application? Primes are a good candidate to be cached. – monksy May 30 '11 at 20:14
  • 2
    A command line program that terminates after one query would be an obvious example. In any case, keeping global state is ugly and should always be considered a trade-off. And I would go so far as to say the sieve (generated at runtime) is essentially useless. If your prime candidates are small enough that you can fit a sieve that size in memory, you should just have a `static const` bitmap of which numbers are prime and use that, rather than filling it at runtime. – R.. GitHub STOP HELPING ICE May 31 '11 at 00:01
  • Or, as a general rule, memoization is almost always useless unless you can somehow omit large spans, since whenever you can afford that much memory, you could even more easily afford that much disk space/`.text` segment size. – R.. GitHub STOP HELPING ICE May 31 '11 at 00:03
  • 1
    Sieve of Eratosthenes is a good (well, good-ish) way of solving the problem "generate all of the primes up to *n*". It's a wasteful way of solving the problem "is *n* prime?" – hobbs Jun 10 '14 at 05:44
  • The Sieve of Eratosthenes is only "Embarrassingly Parallel" for MIMD not for SIMD. A modern computer uses several technologies for parallelization and MIMD is only one of them. If you find a way to make it parallel for SIMD please let me know! – Z boson Apr 29 '15 at 08:14
18

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; 
        }
    }
    
Nathan Parker
  • 308
  • 2
  • 14
Blackhat002
  • 598
  • 4
  • 12
  • 3
    you should return `0` when (number == 1), as 1 isn't a prime number. – Ahmad Ibrahim May 10 '16 at 02:02
  • 1
    These kind of optimisations are IMO irrelevant for this task : why stop at the form _6k ± 1 except 2 and 3_, which revrites in _n^2 mod 6 = 1_, when you can have _n^4 mod 30 = 1 except 2,3,5_ ... in fact, you can go forever because you're using prime numbers to do this optimisation ... and this IS the very principle of the more general Sieve of Eratosthenes algorithm :) – ghilesZ Apr 09 '17 at 10:20
  • 2
    @GhilesZ: I disagree, this is very relevant to the problem and with a single "||" allows the basic loop to run effectively 3 times faster. – verdy_p Aug 15 '17 at 21:18
  • In addition for number==1 it is correctly returning 0 (non-prime) with the tested confition "(number%2==0)", si there's no bug at all – verdy_p Aug 15 '17 at 21:20
  • The Eratosthene methoid is a completely different method that requires allocating a large O(n) array of booleans and it will not necessarily be faster due to indexed accesses. This code is fine as it optimizes first the case of the two first primes 2 and 3 (that's why the loop steps by 2*3). – verdy_p Aug 15 '17 at 21:32
  • You could continue using the same technic using the next prime (5) by steps of (30 == 2*3*5) and tests 9 candidate values (30k+{1;7;11;13;17;19;21;23;29}), still without using an array for the crible However I think it is faster to precompute sqrt(i) once instead of computing product i*i at each loop. – verdy_p Aug 15 '17 at 21:32
  • 1
    (edit: remove 30k+21 which is divisible by 3). You see that the first optimization on the two first primes gains a lot (6 times less loops testing 2 candidates, i.e. 3 times faster than loops on all odds). The optimization on the first 3 primes (30 times less loops testing 8 candidates, will be i.e. 30/8=3.75 times faster than loops on all odds), but the gain is less impressive. This is general: you won't be significantly faster with a generic crible! – verdy_p Aug 15 '17 at 21:42
10
  1. Build a table of small primes, and check if they divide your input number.
  2. If the number survived to 1, try pseudo primality tests with increasing basis. See Miller-Rabin primality test for example.
  3. 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.
Eric Bainville
  • 9,738
  • 1
  • 25
  • 27
  • 4
    +1: complete overkill for what the questioner was asking, but correct nonetheless. – Stephen Canon Oct 08 '09 at 21:54
  • Note that [Guy L.](http://stackoverflow.com/users/1344896/guy-l) recently suggested using Miller-Rabin in an [answer](http://stackoverflow.com/a/31759873/15168) too, and linked to http://rosettacode.org/wiki/Miller-Rabin_primality_test#C — which shows an implementation in C using [GMP](http://gmplib.org/). The entry also has a number of implementations in a wide variety of other languages too. – Jonathan Leffler Aug 02 '15 at 00:09
4

this program is much efficient for checking a single number for primality check.

bool check(int n){
    if (n <= 3) {
        return n > 1;
    }

    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }
        int sq=sqrt(n); //include math.h or use i*i<n in for loop
    for (int i = 5; i<=sq; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • 1
    To test a prime, you should go all the way from `i=2` to `i<=ceil(sqrt(n))`. You missed 2 numbers in your test: First, cast to `(int)` makes `sqrt(n)` trunk the decimals. Second, you used `i – DrBeco Aug 01 '15 at 02:40
  • @DrBeco nice observation! thanks for example. updated the code. – GorvGoyl Aug 01 '15 at 18:49
  • 3
    After analyzing carefully the `ceil()` problem, I realized that although lots of sites recommend it, its just overkill. You can trunk and test just `i<=sqrt(n)` and it will be ok. The test cases are large tween primes. Example: `86028221*86028223=7400854980481283` and `sqrt(7400854980481283)~86028222`. And the smaller know tween primes, `2` and `3`, gives `sqrt(6)=2.449` that trunked will still leave `2`. (But smaller is not a test case, just a comparison to make a point). So, yes, the algorithm is correct now. No need to use `ceil()`. – DrBeco Aug 02 '15 at 06:35
3

Check the modulus of each integer from 2 up to the root of the number you're checking.

If modulus equals zero then it's not prime.

pseudo code:

bool IsPrime(int target)
{
  for (i = 2; i <= root(target); i++)
  {
    if ((target mod i) == 0)
    {
      return false;
    }
  }

  return true;
}
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • 2
    Of course, the downside is that the the sqrt is calculated on every iteration, which will slow it down a lot. – Rich Bradshaw Oct 08 '09 at 16:23
  • 9
    Any reasonable compiler should be able to detect that root(target) is a loop invariant and hoist it. – Stephen Canon Oct 08 '09 at 16:29
  • 1
    (and if you have a compiler that can't do that optimization, you should absolutely file a bug to let the compiler writer know that they're missing this optimization.) – Stephen Canon Oct 08 '09 at 16:30
  • along with many other potential (micro)optimistations, If you manually get the sqrt before the for statement you can check the mod of that as well (and return false if 0). – Matt Lacey Oct 09 '09 at 07:49
  • 2
    What if the target value is 1? – ffffff01 Dec 05 '12 at 20:10
3

After reading this question, I was intrigued by the fact that some answers offered optimization by running a loop with multiples of 2*3=6.

So I create a new function with the same idea, but with multiples of 2*3*5=30.

int check235(unsigned long n)
{
    unsigned long sq, i;

    if(n<=3||n==5)
        return n>1;

    if(n%2==0 || n%3==0 || n%5==0)
        return 0;

    if(n<=30)
        return checkprime(n); /* use another simplified function */

    sq=ceil(sqrt(n));
    for(i=7; i<=sq; i+=30)
        if (n%i==0 || n%(i+4)==0 || n%(i+6)==0 || n%(i+10)==0 || n%(i+12)==0 
           || n%(i+16)==0 || n%(i+22)==0 || n%(i+24)==0)
            return 0;

        return 1;
}

By running both functions and checking times I could state that this function is really faster. Lets see 2 tests with 2 different primes:

$ time ./testprimebool.x 18446744069414584321 0
f(2,3)
Yes, its prime.    
real    0m14.090s
user    0m14.096s
sys     0m0.000s

$ time ./testprimebool.x 18446744069414584321 1
f(2,3,5)
Yes, its prime.    
real    0m9.961s
user    0m9.964s
sys     0m0.000s

$ time ./testprimebool.x 18446744065119617029 0
f(2,3)
Yes, its prime.    
real    0m13.990s
user    0m13.996s
sys     0m0.004s

$ time ./testprimebool.x 18446744065119617029 1
f(2,3,5)
Yes, its prime.    
real    0m10.077s
user    0m10.068s
sys     0m0.004s

So I thought, would someone gain too much if generalized? I came up with a function that will do a siege first to clean a given list of primordial primes, and then use this list to calculate the bigger one.

int checkn(unsigned long n, unsigned long *p, unsigned long t)
{
    unsigned long sq, i, j, qt=1, rt=0;
    unsigned long *q, *r;

    if(n<2)
        return 0;

    for(i=0; i<t; i++)
    {
        if(n%p[i]==0)
            return 0;
        qt*=p[i];
    }
    qt--;

    if(n<=qt)
        return checkprime(n); /* use another simplified function */

    if((q=calloc(qt, sizeof(unsigned long)))==NULL)
    {
        perror("q=calloc()");
        exit(1);
    }
    for(i=0; i<t; i++)
        for(j=p[i]-2; j<qt; j+=p[i])
            q[j]=1;

    for(j=0; j<qt; j++)
        if(q[j])
            rt++;

    rt=qt-rt;
    if((r=malloc(sizeof(unsigned long)*rt))==NULL)
    {
        perror("r=malloc()");
        exit(1);
    }
    i=0;
    for(j=0; j<qt; j++)
        if(!q[j])
            r[i++]=j+1;

    free(q);

    sq=ceil(sqrt(n));
    for(i=1; i<=sq; i+=qt+1)
    {
        if(i!=1 && n%i==0)
            return 0;
        for(j=0; j<rt; j++)
            if(n%(i+r[j])==0)
                return 0;
    }
    return 1;
}

I assume I did not optimize the code, but it's fair. Now, the tests. Because so many dynamic memory, I expected the list 2 3 5 to be a little slower than the 2 3 5 hard-coded. But it was ok as you can see bellow. After that, time got smaller and smaller, culminating the best list to be:

2 3 5 7 11 13 17 19

With 8.6 seconds. So if someone would create a hardcoded program that makes use of such technique I would suggest use the list 2 3 and 5, because the gain is not that big. But also, if willing to code, this list is ok. Problem is you cannot state all cases without a loop, or your code would be very big (There would be 1658879 ORs, that is || in the respective internal if). The next list:

2 3 5 7 11 13 17 19 23

time started to get bigger, with 13 seconds. Here the whole test:

$ time ./testprimebool.x 18446744065119617029 2 3 5
f(2,3,5)
Yes, its prime.
real    0m12.668s
user    0m12.680s
sys     0m0.000s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7
f(2,3,5,7)
Yes, its prime.
real    0m10.889s
user    0m10.900s
sys     0m0.000s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11
f(2,3,5,7,11)
Yes, its prime.
real    0m10.021s
user    0m10.028s
sys     0m0.000s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11 13
f(2,3,5,7,11,13)
Yes, its prime.
real    0m9.351s
user    0m9.356s
sys     0m0.004s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11 13 17
f(2,3,5,7,11,13,17)
Yes, its prime.
real    0m8.802s
user    0m8.800s
sys     0m0.008s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11 13 17 19
f(2,3,5,7,11,13,17,19)
Yes, its prime.
real    0m8.614s
user    0m8.564s
sys     0m0.052s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11 13 17 19 23
f(2,3,5,7,11,13,17,19,23)
Yes, its prime.
real    0m13.013s
user    0m12.520s
sys     0m0.504s

$ time ./testprimebool.x 18446744065119617029 2 3 5 7 11 13 17 19 23 29
f(2,3,5,7,11,13,17,19,23,29)                                                                                                                         
q=calloc(): Cannot allocate memory

PS. I did not free(r) intentionally, giving this task to the OS, as the memory would be freed as soon as the program exited, to gain some time. But it would be wise to free it if you intend to keep running your code after the calculation.


BONUS

int check2357(unsigned long n)
{
    unsigned long sq, i;

    if(n<=3||n==5||n==7)
        return n>1;

    if(n%2==0 || n%3==0 || n%5==0 || n%7==0)
        return 0;

    if(n<=210)
        return checkprime(n); /* use another simplified function */

    sq=ceil(sqrt(n));
    for(i=11; i<=sq; i+=210)
    {    
        if(n%i==0 || n%(i+2)==0 || n%(i+6)==0 || n%(i+8)==0 || n%(i+12)==0 || 
   n%(i+18)==0 || n%(i+20)==0 || n%(i+26)==0 || n%(i+30)==0 || n%(i+32)==0 || 
   n%(i+36)==0 || n%(i+42)==0 || n%(i+48)==0 || n%(i+50)==0 || n%(i+56)==0 || 
   n%(i+60)==0 || n%(i+62)==0 || n%(i+68)==0 || n%(i+72)==0 || n%(i+78)==0 || 
   n%(i+86)==0 || n%(i+90)==0 || n%(i+92)==0 || n%(i+96)==0 || n%(i+98)==0 || 
   n%(i+102)==0 || n%(i+110)==0 || n%(i+116)==0 || n%(i+120)==0 || n%(i+126)==0 || 
   n%(i+128)==0 || n%(i+132)==0 || n%(i+138)==0 || n%(i+140)==0 || n%(i+146)==0 || 
   n%(i+152)==0 || n%(i+156)==0 || n%(i+158)==0 || n%(i+162)==0 || n%(i+168)==0 || 
   n%(i+170)==0 || n%(i+176)==0 || n%(i+180)==0 || n%(i+182)==0 || n%(i+186)==0 || 
   n%(i+188)==0 || n%(i+198)==0)
            return 0;
    }
    return 1;
}

Time:

$ time ./testprimebool.x 18446744065119617029 7
h(2,3,5,7)
Yes, its prime.
real    0m9.123s
user    0m9.132s
sys     0m0.000s
DrBeco
  • 11,237
  • 9
  • 59
  • 76
  • Bonus: `101`-`199` primals all fail here because `101 % (11+90)`. – vp_arth Aug 31 '16 at 11:29
  • 1
    need to stop at `n%(i+86)` or check `n > i+k` – vp_arth Aug 31 '16 at 11:32
  • Well done, sir. I'll take a look. Thank you. The same problem happens with function `check235()` for primes 7, 11, 13, 17, 19, 23 and 29 – DrBeco Sep 05 '16 at 05:01
  • Solution: need to move these reminders to an array, iterate through and break iteration when `i+arr[k] >= n` – vp_arth Sep 05 '16 at 05:33
  • I thought about that, but I don't want a array, because of the `if` with constants can be better optimized by the compiler. I edited to add an exception and keep the current structure intact. But I agree, with an array can be better to human eyes. – DrBeco Sep 05 '16 at 05:40
2

I would just add that no even number (bar 2) can be a prime number. This results in another condition prior to for loop. So the end code should look like this:

int IsPrime(unsigned int number) {
    if (number <= 1) return 0; // zero and one are not prime
    if ((number > 2) && ((number % 2) == 0)) return 0; //no even number is prime number (bar 2)
    unsigned int i;
    for (i=2; i*i<=number; i++) {
        if (number % i == 0) return 0;
    }
    return 1;
}
Vladimir Kocjancic
  • 1,814
  • 3
  • 22
  • 34
1

Avoid overflow bug

unsigned i, number;
...
for (i=2; i*i<=number; i++) {  // Buggy
for (i=2; i*i<=number; i += 2) {  // Buggy
// or
for (i=5; i*i<=number; i+=6) { // Buggy

These forms are incorrect when number is a prime and i*i is near the max value of the type.

Problem exists with all integer types, signed, unsigned and wider.

Example:

Let UINT_MAX_SQRT as the floor of the square root of the maximum integer value. E.g. 65535 when unsigned is 32-bit.

With for (i=2; i*i<=number; i++), this 10-year old failure occurs because when UINT_MAX_SQRT*UINT_MAX_SQRT <= number and number is a prime, the next iteration results in a multiplication overflow. Had the type been a signed type, the overflow is UB. With unsigned types, this itself is not UB, yet logic has broken down. Interations continue until a truncated product exceeds number. An incorrect result may occur. With 32-bit unsigned, try 4,294,967,291‬ which is a prime.

If some_integer_type_MAX been a Mersenne Prime, i*i<=number is never true.


To avoid this bug, consider that number%i, number/i is efficient on many compilers as the calculations of the quotient and remainder are done together, thus incurring no extra cost to do both vs. just 1.

A simple full-range solution:

bool IsPrime(unsigned number) {
    for(unsigned i = 2; i <= number/i; i++){
        if(number % i == 0){
            return false;
        }
    }
    return number >= 2;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1
int is_prime(int val)
{
   int div,square;

   if (val==2) return TRUE;    /* 2 is prime */
   if ((val&1)==0) return FALSE;    /* any other even number is not */

   div=3;
   square=9;    /* 3*3 */
   while (square<val)
   {
     if (val % div == 0) return FALSE;    /* evenly divisible */
     div+=2;
     square=div*div;
   }
   if (square==val) return FALSE;
   return TRUE;
}

Handling of 2 and even numbers are kept out of the main loop which only handles odd numbers divided by odd numbers. This is because an odd number modulo an even number will always give a non-zero answer which makes those tests redundant. Or, to put it another way, an odd number may be evenly divisible by another odd number but never by an even number (E*E=>E, E*O=>E, O*E=>E and O*O=>O).

A division/modulus is really costly on the x86 architecture although how costly varies (see http://gmplib.org/~tege/x86-timing.pdf). Multiplications on the other hand are quite cheap.

Olof Forshell
  • 3,169
  • 22
  • 28
1

To check if a number is prime i'm using a Miller/Rabin Algorithm.

#include <stdlib.h>

typedef size_t positive_number; // also try __uint128_t

static inline positive_number multiplication_modulo(positive_number lhs, positive_number rhs, positive_number mod) {
    positive_number res = 0; // we avoid overflow in modular multiplication
    for (lhs %= mod, rhs %= mod; rhs; (rhs & 1) ? (res = (res + lhs) % mod) : 0, lhs = (lhs << 1) % mod, rhs >>= 1);
    return res; // <= (lhs * rhs) % mod
}

static int is_prime(positive_number n, int k) {
    positive_number a = 0, b, c, d, e, f, g; int h, i;
    if ((n == 1) == (n & 1)) return n == 2;
    if (n < 51529) // fast constexpr check for small primes (removable)
        return (n & 1) & ((n < 6) * 42 + 0x208A2882) >> n % 30 && (n < 49 || (n % 7 && n % 11 && n % 13 && n % 17 && n % 19 && n % 23 && n % 29 && n % 31 && n % 37 && (n < 1369 || (n % 41 && n % 43 && n % 47 && n % 53 && n % 59 && n % 61 && n % 67 && n % 71 && n % 73 && ( n < 6241 || (n % 79 && n % 83 && n % 89 && n % 97 && n % 101 && n % 103 && n % 107 && n % 109 && n % 113 && ( n < 16129 || (n % 127 && n % 131 && n % 137 && n % 139 && n % 149 && n % 151 && n % 157 && n % 163 && n % 167 && ( n < 29929 || (n % 173 && n % 179 && n % 181 && n % 191 && n % 193 && n % 197 && n % 199 && n % 211 && n % 223))))))))));
    for (b = c = n - 1, h = 0; !(b & 1); b >>= 1, ++h);
    for (; k--;) {
        for (g = 0; g < sizeof(positive_number); ((char*)&a)[g++] = rand()); // random number
        do for (d = e = 1 + a % c, f = n; (d %= f) && (f %= d););
        while (d > 1 && f > 1);
        for (d = f = 1; f <= b; f <<= 1);
        for (; f >>= 1; d = multiplication_modulo(d, d, n), f & b && (d = multiplication_modulo(e, d, n)));
        if (d == 1) continue;
        for (i = h; i-- && d != c; d = multiplication_modulo(d, d, n));
        if (d != c) return 0;
    }
    return 1;
}

The test is to print the first primes :

#include <stdio.h>
int main() {
    // C Fast Iterative Algorithm
    // The First 10,000 Primes
    for (int i = 0 ; i < 104730 ; ++i)
        if (is_prime(i, 20))
            printf("%d %c", i, (i+1) % 10 ? ' ' : '\n');

    if (is_prime(9223372036854775783UL, 12))
        if (is_prime(9223372036854775643UL, 12))
            if (!is_prime(3037000493ULL * 3037000453ULL, 12))
                printf("Done.\n");
}

You can put it into a primes.c file then compile + execute :

gcc -O3 -std=c99 -Wall -pedantic primes.c ; ./a.out ;

This variant of the Fermat test has a polynomial runtime in log(n).
The __uint128_t type may be available with 128-bit integers GCC extension.

0

Using Sieve of Eratosthenes, computation is quite faster compare to "known-wide" prime numbers algorithm.

By using pseudocode from it's wiki (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), I be able to have the solution on C#.

public bool IsPrimeNumber(int val) {
    // Using Sieve of Eratosthenes.
    if (val < 2)
    {
        return false;
    }

    // Reserve place for val + 1 and set with true.
    var mark = new bool[val + 1];
    for(var i = 2; i <= val; i++)
    {
        mark[i] = true;
    }

    // Iterate from 2 ... sqrt(val).
    for (var i = 2; i <= Math.Sqrt(val); i++)
    {
        if (mark[i])
        {
            // Cross out every i-th number in the places after i (all the multiples of i).
            for (var j = (i * i); j <= val; j += i)
            {
                mark[j] = false;
            }
        }
    }

    return mark[val];
}

IsPrimeNumber(1000000000) takes 21s 758ms.

NOTE: Value might vary depend on hardware specifications.

S_R
  • 157
  • 1
  • 4