Palindromic NumbersProblem code: K2 |
All submissions for this problem are available.
The Chef has figured out that there are some numbers which have an interesting property: they are the same when read from left to right, and from right to left. For example, 5115 and 929 are such numbers, while 31 and 125 are not. He calls such numbers palindromic numbers.
After a while, the Chef has realized that his definition of palindromic numbers is not really precise. Whether a number is palindromic or not depends on the base in which the number is written. For example, 21 is not palindromic in base 10 but it is palindromic in base 2 (because 21 = 101012).
The Chef finds it interesting that any number becomes palindromic when it is written in an appropriate base.
Given a number N, write a program to help The Chef compute the smallest base B such that N is palindromic when written in base B.
Input
The first line contains t, the number of test cases (about 1000). Then t test cases follow.
Each test case consists of a number N written in one line (1 <= N <= 1010).
Output
For each given number N, print a line containing the smallest base B such that N is palindromic in base B.
Example
Input:
3
1
4
21
Output:
2
3
2
| Author: | admin |
| Date Added: | 30-11--0001 |
| Time Limit: | 3 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, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, 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

Can anyone give me a cluse as
Can anyone give me a cluse as to wt's wrong in the logic of the following code?
Post new comment
sorry, i meant "clue" :)
sorry, i meant "clue" :)
Please re-read the question.
Please re-read the question. It asks for the smallest base in which the number is a palindrome.
Is runtime error same as
Is runtime error same as failing in the test cases. is it possible to provide test case data.
thanks vivek
Runtime error means your
Runtime error means your program is crashing / throwing an exception during the runtime of your program, as opposed to just printing the wrong answer. You are expected to debug your solution yourself rather than being given the test cases.
getting runtime error with
getting runtime error with this code.can anyone tell me what is wrong with this one.it's working properly in my machine.
public class Main{
public static long compute(String value)throws Exception{
int base_value=2;
long b=Long.parseLong(value);
if(b>=1 && b<=1000000000l){
for(base_value=2;base_value<b;base_value++)
{
String s=Long.toString(b,base_value); // the logic is working with different inputs in my machine
StringBuffer s1=new StringBuffer(s);
String sr=""+s1.reverse();
if(s.equals(sr)==true)
{
return base_value;
}
}
return base_value; //as the largest base can be the (no. -1)
}
else return base_value;
}
public static void main(String args[])throws Exception{
int call=Integer.parseInt((new java.io.BufferedReader(new java.io.InputStreamReader(System.in))).readLine());
if(call<=1000 && call>0 ){
//long[] values=new long[call];
int i=0;
for(i=0;i<call;i++)
{
long b=compute((new java.io.BufferedReader(new java.io.InputStreamReader(System.in))).readLine());
//}
// java.io.PrintWriter out = new java.io.PrintWriter(System.out);
//for(i=0;i<call;i++) {
System.out.println(b);
}
//out.close();
}
}
}
I am comparing the result of
I am comparing the result of mines with the solution shown of the people that got it correct and it seems to
be the same. But I keep getting "wrong answer". I am not sure why. Any help.
Any hints on making the
Any hints on making the program faster. Maybe something that will bypass ( or improve) checking if the number is a palindrome from base 2 and up ? Any theorems ?
@admin : what are the
@admin : what are the possible values for the base. . .is this 2,3 and 10 only(Standard bases). . .or it can be 4,5,6etc. . .also. . .as I think Base of a number system means no. of distinct digits int that no. system. . . . .So we are to consider only standard no. sys's i.e binary,octal and decimal. . . .or it can be any arbitrary ? ? ? and can it be hexadecimal also ? ? ? ?
Any base is allowed. You can
Any base is allowed. You can have base 123 if you want to.
ok. . .then it's fine. .
ok. . .then it's fine. . .ThankYou ! ! !
@admin : please check
@admin : please check this http://discussed.codechef.com/showthread.php?p=2769&posted=1#post2769
@stepehen : please check the
@stepehen : please check the code posted at forums. . ! ! !
@admin: sir please suggest me
@admin: sir please suggest me wht should i do now ? ? ?
@admin can u give me more
@admin
can u give me more testcases ???
Hi admins, Please through
Hi admins,
Please through some light on the possible values of the base.
For example.. if the number is 2134 and the base is 100, what will the value??
Please reply ASAP.
My first submission
My first submission here,pleae help :
in the FAQs it says when i get a runtime error i can check what error by hovering my icon over the icon,
is that the "runtiime error icon" ? if so, i'm not able to figure out whats wrong.
my program works fine on my sys., i'm programming in Java, any debugging suggestions?
My first submission
My first submission here,pleae help :
in the FAQs it says when i get a runtime error i can check what error by hovering my icon over the icon,
is that the "runtiime error icon" ? if so, i'm not able to figure out whats wrong.
my program works fine on my sys., i'm programming in Java, any debugging suggestions?
also, i have tried other test
also, i have tried other test cases.
are there any particularly tricky ones i should watch out for?
The first one you should have
The first one you should have tried was the largest possible one.. which immediately throws an exception.
i'm gettin the answer, on my
i'm gettin the answer, on my sys...but i'm getting a TLE.
in java, what do i do to figure out how long my output's taking?
#include<iostream>#include<st
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
//read:length
int length;
cin>>length;
int *x=new int[length];
int *result=new int[length];
bool ok=false;
for(int i=0;i<length;i++)
{
ok=false;
cin>>x[i];
int j;
for(j=2;!ok;j++)
{
//cout<<"Hellon";
vector<char> num;
int tmp=x[i];
while(tmp)
{
num.push_back('0'+tmp%j);
tmp/=j;
}
int s=num.size();
ok=true;
for(int k=0;k<s/2;k++)
{
//cout<<num[k]<<" "<<num[s-k-1]<<endl;
if(num[k]!=num[s-k-1])
{
ok=false;
break;
}
}
}
//cout<<--j<<endl;
result[i]=--j;
}
for(int i=0;i<length;i++)
{
cout<<result[i]<<endl;
}
return 0;
}
And whats problem thisone ? I tested lots of input :S
@everyone is itoa function
@everyone
is itoa function work on codecehf...........????????????? pls help.............
@Admin: I've tried my
@Admin:
I've tried my solution with many the cases. But I'm still getting wrong answer. Can you tell me for which case my code is giving the wrong answer. My code:
http://www.codechef.com/viewsolution/248382
@stephan can u check why i am
@stephan
can u check why i am getting compilation error..
my code no. is 289809
@Stephen- Sir my solution
@Stephen-
Sir my solution http://www.codechef.com/viewsolution/296482
takes .001 sec for the worst case on my machine..
even if all the 1000 cases are the worst ones it should take one second..
and even if my machine is twice as fast it should not time out..
but it does..
any idea..
thnx..
can anybody tell me the
can anybody tell me the approach to do this problem?? I guess I would have to check each n every base in sequence (2 onwards) but upto which number I would have to check??
Hi! my solution is fast, even
Hi!
my solution is fast, even for worst case of 10^10, but it is giving time limit exceed.
http://www.codechef.com/viewsolution/400231
any ideas??
Your code doesn't actually
Your code doesn't actually work at all as is, since you are scanning an unsigned long long in as an unsigned int, then returning an unsigned long long via a method which should return an int, then printing it as an unsigned int. But anyway, if you fix that:
The worst case input is not necessary the biggest. Have you tried 1000 (or even 1) test cases of, say, 9999998273?
:O so, printf("%llu",x) is
:O
so, printf("%llu",x) is correct command for unsigned long long,
for all numbers(n) do i need to check for all bases starting frm 2 till n-1?
Do you think that will be
Do you think that will be fast enough for the input I suggested?
my code is running fine on my
my code is running fine on my machine...but it is giving a run time error here.....my question is...
can we use static arrays? with 32 as the maximum size ? 32 because maximum number of digits are possible in base 2 as it is stored in memory and one integer occupies only 32 bits of space...
my error is NZEC can there be any other reason for this error?
Of course you can use static
Of course you can use static arrays. The first thing you should have done is test your code on large values of N.. but you clearly haven't :)
what can be the maximum value
@nathanmelfoy: It can be
time limit exceede
Try number 9999999145
Every number is palindrome if
What is wrong with my code..I