My question is about the conditional test in trial division. There seems to be some debate on what conditional test to employ. Let\'s look at the code for this from Roset
An answer to only a small portion of this post.
Case 2 fix to deal with overflow.
#include
int is_prime(unsigned n) {
unsigned p;
if (!(n & 1) || n < 2)
return n == 2;
#define UINT_MAX_SQRT (UINT_MAX >> (sizeof(unsigned)*CHAR_BIT/2))
unsigned limit = n;
if (n >= UINT_MAX_SQRT * UINT_MAX_SQRT)
limit = UINT_MAX_SQRT * UINT_MAX_SQRT - 1;
for (p = 3; p * p < limit; p += 2)
if (!(n % p))
return 0;
if (n != limit)
if (!(n % p))
return 0;
return 1;
}
The limit calculation fails if both sizeof(unsigned) and CHAR_BIT are odd - a rare situation.