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.
i forget the proper c++ syntax, but i will help you with the algorithm
since primes occur less frequently than palindromic numbers, we will test for that property first, to make the program run faster and more efficiently:
to find a prime, start at 2 and try dividing by each integer that is less than itself but greater than one. so,
bool ispossiblyprime=true, isapalindrome=false;
int maybe, N, M, test, leftdigit, rightdigit, numdigits, i;
float prime, bigdivider, smalldivider;
cout%26lt;%26lt;"enter a value between 2 and 100000: ";
cin%26gt;%26gt;N;
maybe=N;
while(isapalindrome==false)
{
maybe--;
//had to do this bc i had to put
//the ++ operation in the beginning of the prime loop
//otherwise it would ruin the answer of the prime loop
while(ispossiblyprime==true)
{
maybe++;
test=2;
while(test%26lt;maybe)
{
if(maybe%test==0)
then
{
ispossiblyprime=false;
break;
}
else
{
test++;
}
} //
} //end of prime loop
prime=maybe; //the maybe that comes out is def prime
numdigits=1+int(log(prime));
i=1;
while(i%26lt;int(numdigits/2.0)) //accounts for the middle number problem
{
bigdivider= pow(10,(numdigits-i));
smalldivider=pow(10,i);
leftdigit= int(prime/divider);
rightdigit= ((10*prime)/smalldivider)- (10*int(prime/smalldivider));
if(leftdigit!=rightdigit)
then
{
isapalindrome=false
maybe++;
break;
}
else
{
i++;
isapalindrome=true;
}
}
}
M=prime;
cout%26lt;%26lt;"\nThe smallest palindromic prime greater ";
cout%26lt;%26lt;"than or equal to "%26lt;%26lt;N%26lt;%26lt;" is "%26lt;%26lt;M%26lt;%26lt;".\n";
wow most of it came back to me
hope this helps
ps i worked on this for 2 hours!!!! (still, i did it for fun BUT)
PLEASE give me the 10 pts lol
Reply:set M = N
Loop:
Is M palindrome? If YES, check for prime ELSE increment M and loop
Prime: If Prime exit with value ELSE return to loop (where increment happens)
hollyhock
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment