IARCS (Prime Palindromes)


Question :
http://www.iarcs.org.in/inoi/contests/sep2004/Basic-1.php

Solutions :


#include <stdio.h>
#include <math.h>
#include <string.h>

bool is_prime (int x)
{
    int length = (int) sqrt (x);

    for ( int i = 2; i <= length; i++ ) {
        if ( x % i == 0 )
            return false;
    }

    return true;
}

bool is_palindorme (int x)
{
    char a [8];
    sprintf (a, "%d", x);

    int length = strlen (a);

    for ( int i = 0; i < length / 2; i++ ) {
        if ( a [i] != a [length - 1 - i] )
            return false;
    }

    return true;
}

int main ()
{
    int N;
    scanf ("%d", &N)

    if ( N % 2 == 0 ) // N even
        N++;

    while( true ) {
        if ( is_palindorme (N) && is_prime (N) ) {
            printf ("%d\n", N);
            break;
        }

        N += 2;
    }
    return 0;
}

Advertisements

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