Skip to content

Instantly share code, notes, and snippets.

@mingyc
Created February 29, 2012 04:49
Show Gist options
  • Save mingyc/1937849 to your computer and use it in GitHub Desktop.
Save mingyc/1937849 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#define MAX 1000010
int prime[MAX] = {0};
void make_prime();
int reverse(int);
int main()
{
int n;
make_prime();
while (scanf("%d", &n) == 1)
{
if (!prime[n])
{
if (n != reverse(n) && !prime[reverse(n)])
printf("%d is emirp.\n", n);
else
printf("%d is prime.\n", n);
}
else
printf("%d is not prime.\n", n);
}
return 0;
}
void make_prime()
{
int x, y, sqrtM;
for (x = 2, sqrtM = sqrt(MAX); x < sqrtM; x++)
if (!prime[x])
for (y = x*2; y < MAX; y+=x)
prime[y] = 1;
}
int reverse(int a)
{
int x, y, ap;
for (x = 10; a/x > 9; x*=10);
for (ap=0, y=1, x=(a<10?1:x); a>0 && x; ap+=(a/x)*y, a%=x, y*=10, x/=10);
return ap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment