Prime palindrome TLE

This code is showing results for large numbers also.Still i am getting tle.

#include <stdio.h>
#include <conio.h>
int main(void)
{
   int a,rev=0,dig,b,i,count=0;
    
    scanf("%d",&b);
    while(1)
  {  rev=0;
     a=b;
    while(a!=0)
    {
       dig=a%10;
       rev=(rev*10)+dig;
       a=a/10;
    }
    if(rev==b){
               for(i=1;i<=b;i++)
               {
                    if(b%i==0){count++;}
               }
               if(count==2){break;}
               }
                              
    count=0;
    b++;
} 
   printf("%d",b);
    
    getch();
    return 0;
    
}

First of all, add this header file:

#include<math.h>

Now, in this code:

if(rev==b){
               for(i=1;i<=b;i++)
               {
                    if(b%i==0){count++;}
               }
               if(count==2){break;}
}

Make the following changes:

if(rev==b){
               for(i=2;i<= sqrt(b);i++)
               {
                    if(b%i==0){count++;i = sqrt(b)+1;}
               }
               if(count==1){break;}
}

Why check all numbers when you have already encountered one? Now your code will work fine.

1 Like

I am not getting why {i=sqrt(b)+1} if i divides b.and where is count?

Oh yeah. Increment count by one and then do this line. I’ve edited it now