Monday, May 24, 2010

Write C/C++ program whre input will an integer N ≤ 100000 and output M ≥ N such that M is a Palindrome Prime.

An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes.





Write the Algorithm or C program whre input will an integer N, 1 ≤ N ≤ 1000000 and output should be the smallest integer M ≥ N such that M is a prime number and M is a palindrome.


For example, if N is 31 then the answer is 101.

Write C/C++ program whre input will an integer N ≤ 100000 and output M ≥ N such that M is a Palindrome Prime.
#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;math.h%26gt;





int isPalindrom(unsigned int a) {


unsigned int i, n;


char str[15];





sprintf(str, "%d", a);


n = strlen(str) - 1;


i = 0;





for (i = 0; i %26lt;= n / 2; ++i) {


if (str[i] != str[n - i]) return 0;


}





return 1;


}








// Using Seive


#define LIMIT 1003002


unsigned int primes[LIMIT] = { 0 };





void enrichPrimes() {


unsigned int i;


for (i = 2; i %26lt; LIMIT; ++i)


primes[i] = 1;





for (i = 2; i %26lt;= sqrt(LIMIT); ++i)


if (primes[i]) {


unsigned int n = i * i;


while (n %26lt; LIMIT) {


primes[n] = 0;


n += i;


}


}


}





unsigned int findSmallestPalindrom(unsigned int num) {


unsigned int value = num;


while (value %26lt; LIMIT) {


if (primes[value] %26amp;%26amp; isPalindrom(value))


return value;


++value;


}


return 0;


}





int main(void) {


int input;





enrichPrimes();





printf("Enter a number (-1 to stop): ");


scanf("%d", %26amp;input);





while (input %26gt;= 0) {


printf("The following palindrom prime is %d\n\n",


findSmallestPalindrom(input));


printf("Enter a number (-1 to stop): ");


scanf("%d", %26amp;input);


}





return 0;


}
Reply:// This works too


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;sstream%26gt;


#include %26lt;math.h%26gt;





#define MAX_LIMIT1003001





using namespace std;





bool isPrime(long iInput)


{


bool bResult = true;





if(iInput %26gt;= 1 %26amp;%26amp; iInput %26lt;= 3)


bResult = true;


else if (iInput % 2 != 0)


{


long iMaxTest = (long) sqrt((double) iInput);





for( long i = 3; i %26lt;= iMaxTest; i++)


{


if(iInput % i == 0)


{


// Leave loop, not prime


bResult = false;


i = iMaxTest + 1;


}


}


}


else


{


bResult = false;


}





return bResult;


}





bool isPalindrome(long iInput)


{


stringstream oInput;


string sInput;


string sTest;





// Convert to string


oInput %26lt;%26lt; iInput;


oInput %26gt;%26gt; sInput;





for( long i = sInput.length() - 1; i %26gt;= 0; i-- )


sTest.push_back(sInput[i]);





return (sInput.compare(sTest) == 0);


}





void main( void )


{


long iInput = 0;


bool bFound = false;





cout %26lt;%26lt; "Enter Integer: ";


cin %26gt;%26gt; iInput;





if(iInput %26lt;= 1000000 %26amp;%26amp; iInput %26gt;= 1)


{


while(!bFound %26amp;%26amp; iInput %26lt;= MAX_LIMIT)


{


if(isPalindrome(iInput) %26amp;%26amp;


isPrime(iInput))


{


cout %26lt;%26lt; "Result: " %26lt;%26lt; iInput %26lt;%26lt; endl;


bFound = true;


}


else


iInput++;


}





if(!bFound)


cout %26lt;%26lt; "No result" %26lt;%26lt; endl;


}


else


cout %26lt;%26lt; "Invalid input" %26lt;%26lt; endl;


}


No comments:

Post a Comment