Prime PalindromesProblem code: PRPALIN |
All submissions for this problem are available.
An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 N 1000000. You must find 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.
Input
A single integer N, (1 N 1000000), on a single line.
Output
Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N.
Example
Input: 31 Output: 101
| Author: | admin |
| Date Added: | 28-07-2009 |
| Time Limit: | 1 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
HELP
Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:
- Accepted
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark. - Time Limit Exceeded
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach. - Wrong Answer
Your program compiled and ran succesfully but the output did not match the expected output. - Runtime Error
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section. - Compilation Error
Your code was unable to compile. When you see this icon, click on it for more information.
If you are still having problems, see a sample solution here.

Fetching successful submissions

why can't i submit my
Why cant I view the problem
Why cant I view the problem ??
There is something wrong with
There is something wrong with the test cases. This will be fixed in a few days.
M is less than or equal to N.
M is less than or equal to N. I had to find that out by trial-and-error. Why doesn't your software permit you to enter numeric symbols? Since most of the problems are numeric in nature, this is quite a handicap. I notice you have fixed some of them, though. Thank you.
how to remove the time limit
how to remove the time limit exceeded error in this program.............
You must find the smallest
You must find the smallest integer M N such that M is a prime number and M is a palindrome.
can u please explain the meaning of underlined text ! ! !
i.e how M is realted to N ? ? ? ?
It should say M >= N, as John
It should say M >= N, as John pointed out in the comment 2 above yours.
yeah. . i got it. . .nyways
yeah. . i got it. . .nyways thnx fr replyin Stephen ! ! !
@Stephen It should say M >= N
@Stephen
It should say M >= N to be coherent with the example.
Whoops, thanks. Fixed.
Whoops, thanks. Fixed.
@ALL anytest case/s which
@ALL
anytest case/s which takes time??
Hey all, My program is
Hey all,
My program is running for small test cases but as the value is increased it is unable to show the result.The best that it went upto was around 98000.Someone please rectify the mistake.
Here's the code...
#include<stdio.h>
int main()
{
unsigned long int n,rev,i,j,k,m=0;
unsigned long int fl=1;
rev=0;
printf("Enter the number:");
scanf("%d",&n);
for(i=n;;i++)
{
fl=1;
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
fl=0;
}
}
if(fl==1)
{
m=i;
while(m>0)
{
k=m%10;
rev=(rev*10)+k;
m/=10;
}
if(rev==i)
{
break;
}
else
rev=0;
}
}
printf("The required number is: %d",rev);
return 0;
}
@admin............plz check
@admin............plz check the sol
http://www.codechef.com/viewsolution/246369
i m getting TLE ........but it is working fine on my system
HELP HELP HELP
@Admin: I've tried a lot to
@Admin:
I've tried a lot to reduce the time, I've managed to reduce the time to check if the number is prime to a bre minimum, I've taken tth least time to compute if its a palindrome or not. Can you please tell me which part is taking time, because I've tried tor educe all parts. My code:
http://www.codechef.com/viewsolution/247098
Thank you.
@vikas - your code doesn't
@vikas - your code doesn't work for the sample input.
@Manjot - try input 99000.
i removed that
i removed that error........and also tried to optimise the code to a great extent............then also i am getting TLE.......plz tell me .........where my code is lacking ..........plz help me.......this is optimised one.....
http://www.codechef.com/viewsolution/247548 plz check it..........
sir..plz check my
sir..plz check my solution.........and tell me the bug in solution,,,,,,,,,,http://www.codechef.com/viewsolution/247548
http://www.codechef.com/views
http://www.codechef.com/viewsolution/247548
@admin -> its really
@admin -> its really frustrating...........so many TLEs.......
may i know that....which part of my code or for which input case my code is taking such a long time???????
any hint ??????????????
my code->http://www.codechef.com/viewsolution/268644
thank you.............
i was doing the stupid
i was doing the stupid mistake........but finally goi it accepted...........
thnx for not answering my questions...........
Can anyone tell what must be
Can anyone tell what must be the output for 99000.....
@Anish 1003001 is the output
@Anish 1003001 is the output for 99000
@Piyush:- Yep got it...
@Piyush:- Yep got it... Thanks man....
Hello admin, I'm not
Hello admin,
I'm not understanding why I'm getting the wrong answer, I have checked for the boundary numbers too.
What about single digit prime number? Please reply , where my code is giving the wrong answer.
please give me a hint atleast, as it is not a contest problem now.
Thanks...
I got it correct.... But
I got it correct....
But before, I was using sieve of Erathmosis method to generate prime upto 1000000 and then storing the palindromes in an array. This was giving me a SIGGEV error.
Then I just changed the code that had only the valid prime palindromes.. and this worked ... But I didn't get why the previous code was giving that particular error ????
Thanks
what will be the output if
what will be the output if number is greater then 98689
what should be prined after
what should be prined after 98689 because the answer is 1003001 which is less tha 1000000.....
pls reply.....
thanx.
The problem statement just
The problem statement just says N is less than a million, it doesn't say your answer must be.
please check my code why it
please check my code why it is giving wrong answer
http://www.codechef.com/viewsolution/301604
thanx ...
I m nt able to submit my
I m nt able to submit my code.....Its showing "Wrong Login & Password"
can any one please tell me
can any one please tell me where i am going wrong
http://www.codechef.com/viewsolution/301604
please reply....
Whats wrong with my code can
Whats wrong with my code can any one tell me please i submitted lot of solution...
http://www.codechef.com/viewsolution/317757
please help...
Hello Admin, I am getting
Hello Admin,
I am getting Runtime Error.But am not able to find out why.!
I take input in a string type and first check for palindrome then check if that palindrome is a prime or not.
Please help.!
My code is:
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
string s;int c=9;string str;
int palindrome()
{
int k=0;int count=0;
while(1)
{
if(s[count]=='0')
count++;
else
break;
}
int i=count;//cout<<"count"<<(31-count)/2+count;
while(i<=((c-count)/2+count))
{
if(s[i]!=s[c-i+count])
{
k=1;
break;
}
i++;
}
if(k==1)
{return 1;}
else
return 0;
}
int increment()
{
if(s[c]=='9')
{
s[c]='0';c--;
increment();
}
else
{
s[c]=(char)((int)s[c]+1);//cout<<"h";//cout<<s[c];
return 0;
}
}
int prime()
{
int k=0;int count=0;
while(1)
{
if(s[count]=='0')
count++;
else
break;
}
int i=count;
int number=0;
//cout<<"count="<<count;
while(i<=c)
{
number=number*10+ (((int)s[i])-48);
i++;
}
int g=0;
int root=pow(number,0.5);
for(int ii=2;ii<root;ii++)
{
if(number%ii==0)
{
g=1;break;
}
}
if(g==1)
return 0;
else return number;
}
int main()
{
cin>>str;int m=str.length()-1;int i=c;
//cout<<c;
for( i=c;i>c-str.length();i--)
{s[i]=str[m];m--;}
for(;i>=0;i--)
s[i]='0';
while(1)
{
int chk=palindrome();
if(chk==0)//palindrome
{
int p=prime();
if(p!=0)
{
cout<<p;
break;//found the prime
}
}
int tt=increment();c=9;
}
system("pause");
return 0;
}
I got it!Thanks!
I got it!Thanks!
1s for this problem is
1s for this problem is calculated for single N or running the code several times for different N's?
This is explained in the FAQ.
This is explained in the FAQ. In this case, each input file only contains one N, so the time limit only applies to that one N.
I am getting a runtime(other)
I am getting a runtime(other) error.According to the FAQ it is generally caused by using too much memory. But I don't think that's the problem this time around. Its giving the correct answer for all test cases that I checked on my machine.
Please take a look and point out the mistake
http://www.codechef.com/viewsolution/398600
hello admin plz check my code
hello admin plz check my code plz
#include<stdio.h>
# define max 1000000
int main()
{
int n,i,num,temp,count=0,j,remainder=0,sum=0;
printf("enter initial limit:");
scanf("%d",&n);
for(i=n;i<max;i++)
{
num=temp=i;
while(num>0)
{
remainder=num%10;
num=num/10;
sum=sum*10+remainder;
}
if(sum==temp)
{
for(j=2;j<temp;j++)
{
if(temp%j==0)
count=1;
}
if(!count)
{
printf("%dn",temp);
break;
}
}
sum=0;
count=0;
}
return 0;
}
it is finely working.plz help
it is finely working.plz help i can't figure out what is the problem in this code.codechef compiler is giving wrong answer
Please read the FAQ.
Please read the FAQ.
/sources/tested.c: In
/sources/tested.c: In function 'main': /sources/tested.c:7: error: expected ')' before ';' token
WTF....?
what is the problem in my program?
and after correction it is showing time limit exceed......
can nebody tell wats wrong
can nebody tell wats wrong with my program? a runtime error is encountered.
#include<stdio.h>
#include<math.h>
//#include<conio.h>
int prime(long int num)
{
int i;
if(num==2) return(1);
else if(num%2==0) return(0);
else
{for(i=3;i<(sqrt(num));i=i+2)
{
if(!(num%i))
return(0);
}
return(1);}
}
int palin(long int rev,long int num)
{
long int temp=num;
while(num>0)
{rev=(rev*10)+(num%10);
num=num/10;}
if(temp==rev)
return(1);
else
return(0);
}
void main()
{
int pc,pac=0,rev=0;
long int num;
scanf("%ld",&num);
while(!pac)
{
pc=prime(num);
if(pc)
{pac=palin(rev,num);
if(pac)
printf("%ld",num);
}
num+=1;
}
//getch();
}
plz help me . y this code is
plz help me . y this code is printing garbage value in TURBOC
int main()
{
unsigned long int i=1000000;
printf("nnnn hello =%ld",i);
return 0;
}
@Raman Tripathi the format
@Raman Tripathi the format specifier '%ld' is wrong, chk the link. '%ld' is for signed long integer and not for unsigned.
There's no max limit
There's no max limit specified in problem, but from (1 N 1000000) we assume that max limit is 1000000, but answers will be tested beyond this case.
I've also found a list of Palindromic Primes, you can check your answers are correct or not from here:
http://oeis.org/A002385/b002385.txt
Hey all, My program is
Hey all,
My program is running for small test cases but as the value is increased it is unable to show the result.The best that it went upto was around 98000.Someone please rectify the mistake.
Here's the code...
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{unsigned int flag=1,num,digit;
unsigned int n,b;
cin>>b;
for(unsigned int i=b;i<1000000;i++)
{
flag=1;
for(unsigned int j=2;j<i;j++)
{
if(i%j==0)
{
flag=0;
break;
}
} if(flag==1)
{
// cout << i << " ";
num=i;
n=i;
unsigned int rev=0;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}while(num!=0);
if(n==rev)
{cout<<n<<" ";break;}
}
} system("pause");
return 0;
}
hey i dunno why for 99000 or
#include int Palin(long long
the link is
@hungrycoder::Try to think
@javadecoder i dont
no no sry....its showing the
My program is working
i m getting wrong
i m getting wrong answer.....
i m getting wrong answer.....
i tried doing but am getng
@admin i m getting wrong
#include #include main() { in
plz any1 help me..my code is
hey all!!! i wanted to share
@abhi6691 use this
i'm kinda newbie in this, cn
http://pastebin.com/MtkewU7t
@worm thnx man!!!
@katherine add a return 0 at
shudn't the output for 99000
srry for above...i forgot the
hey are the single digit no's
my code is running properly
Hi guys..... Most of us
can anyone tell me what is
@rgaut_89 : Thanx man u are a
Hey admin, my program uses
@admin: i am getting wrong
i hate how ALOT of users are
#include int main() { long
i am not understanding that
have the test cases been
I tried the sample input in
@Admin..can u pls help me
pls look at my