ACM (UVa) : 10235


// http://uva.onlinejudge.org/external/102/10235.html

#include <stdio.h>
#include <stdlib.h>

bool prime [1000005];

int main ()
{
    for ( int i = 2; i < 1000005; i++ )
        prime [i] = true;

    for ( int i = 2; i < 1000004; i++ ) {
        for ( int j = i * 2; j < 1000004; j += i )
            prime [j] = false;
    }

    int n;

    while ( scanf ("%d", &n) != EOF ) {

        if ( prime [n] == false )
            printf ("%d is not prime.\n", n);

        else {
            char reverse [10];
            int i = 0;
            int save_n = n;

            while ( n ) {
                reverse [i++] = (n % 10) + '0';
                n /= 10;
            }

            reverse [i] = 0;

            n = atoi (reverse);

            if ( prime [n] == true && n != save_n )
                printf ("%d is emirp.\n", save_n);

            else
                printf ("%d is prime.\n", save_n);
        }
    }

    return 0;
}

One thought on “ACM (UVa) : 10235

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s