Skip to content

Instantly share code, notes, and snippets.

@dsturnbull
Created August 20, 2010 05:45
Show Gist options
  • Save dsturnbull/539691 to your computer and use it in GitHub Desktop.
Save dsturnbull/539691 to your computer and use it in GitHub Desktop.
bool
is_prime(int num)
{
int i, w;
if (num <= 1)
return false;
if (num == 2 || num == 3)
return true;
if (num % 2 == 0)
return false;
if (num % 3 == 0)
return false;
if (bsearch(&num, primes, num_primes, sizeof(int), comp_prime))
return true;
i = primes[num_primes - 1];
if (num < i)
return false;
w = 2;
while (i * i <= num) {
if (num % i == 0)
return false;
i += w;
w = 6 - w;
}
return true;
}
int
comp_prime(const void *a, const void *b)
{
if (*(int *)a > *(int *)b) return 1;
if (*(int *)a < *(int *)b) return -1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment