# Problem With Max Limit of n.

PRPALIN For this question, i have pasted my solution below.
I am getting correct output for all values of n <100000 after that i am not getting correct answer.
Can someone help me out.

``````#include<stdio.h>
int f1(int n);
int palind(int n);
int main(){
int n;
scanf("%d",&n);
printf("%d",f1(n));

return 0;
}
int f1( int n)
{

while(n<1000000){
int k,flag=1,palin;
if ( ( (!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3) )
flag=0;

if(flag==1){
for(k=1;36*k*k-12*k<n;++k){
if((n%(6*k+1))==0||(n%(6*k-1)==0))
flag=0;
}
}
if(flag==1){
palin=palind(n);
if(palin==1)
return n;
}

n++;
}
}
int palind( int n)
{

int sum=0,num=n;
while(n>0){

sum=sum*10+n%10;

n/=10;

}

if(sum==num)
return 1;
else
return 0;

}
``````

The problem statement says, that N <= 10^6. However, there is no guarantee, that the result will be <= 10^6. In fact, there are no prime palindromes between 10^5 and 10^6, so for any number in this range, the correct output is 1003001, which is greater than 10^6.

To solve your problem, replace while (n < 1000000) by while (1). This will lead to correct result, but you will end up with TLE, because your algorithm is too slow. As there are just a couple of prime palindromes in the requested range, try precalculating them and store them directly to source code.