CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Palindromic Numbers

Palindromic Numbers

Problem code: K2

  • Submit
  • All Submissions

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


  • Submit

Comments

  • Login or Register to post a comment.

Can anyone give me a cluse as

ankit0311 @ 15 Dec 2009 07:33 PM

Can anyone give me a cluse as to wt's wrong in the logic of the following code?

 

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. int pal(char stack[])
  6. {
  7. int i,n;
  8. n=strlen(stack);
  9. for(i=0;i<n/2;i++)
  10. {
  11.   if(stack[i]!=stack[n-i-1])
  12.   return 0;
  13. }
  14. return 1;
  15. }
  16.  
  17.  
  18. main()
  19. {
  20. int t,i,j,top;
  21. int* res;
  22. long num,n;
  23. char stack[200];
  24. scanf("%d",&t);
  25. res=(int*)malloc(t*sizeof(int));
  26. for(i=0;i<t;i++)
  27. {
  28.   scanf("%ld",&num);
  29.   for(j=2;;j++)
  30.   {
  31.    top=-1;
  32.    n=num;
  33.    while(n)
  34.    {
  35.     stack[++top]=n/j;
  36.     n=n/j;
  37.    }
  38.    if(pal(stack))
  39.    {
  40.    res[i]=j; 
  41.    break;
  42.    }
  43.   }
  44. }
  45. for(i=0;i<t;i++)
  46. printf("%dn",res[i]);
  47. return 0;
  48. } 

 

Language: 
C(gcc 4.3.2)

Post new comment

sorry, i meant "clue" :)

ankit0311 @ 15 Dec 2009 07:35 PM

sorry, i meant "clue" :)

Please re-read the question.

admin @ 15 Dec 2009 07:53 PM

Please re-read the question. It asks for the smallest base in which the number is a palindrome.

Is runtime error same as

viyyer @ 22 Dec 2009 10:26 PM

Is runtime error same as failing in the test cases. is it possible to provide test case data.

thanks vivek

Runtime error means your

triplem @ 23 Dec 2009 03:27 AM

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

suvro @ 24 Dec 2009 04:37 PM

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

TrialAndError @ 25 Dec 2009 06:04 AM

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

TrialAndError @ 26 Dec 2009 12:25 AM

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

Geniusguy @ 10 Jan 2010 11:38 PM

@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

triplem @ 11 Jan 2010 01:57 AM

Any base is allowed. You can have base 123 if you want to.

ok. . .then it's fine. .

Geniusguy @ 11 Jan 2010 08:54 AM

ok. . .then it's fine. . .ThankYou !  !  !

@admin : please check

Geniusguy @ 13 Jan 2010 01:11 AM

@admin : please check this http://discussed.codechef.com/showthread.php?p=2769&posted=1#post2769

@stepehen : please check the

Geniusguy @ 13 Jan 2010 10:20 AM

@stepehen : please check the code posted at forums. . ! ! !

@admin: sir please suggest me

Geniusguy @ 13 Jan 2010 07:08 PM

@admin: sir please suggest me wht should i do now ? ? ?

@admin can u give me more

lipun4u @ 2 Feb 2010 04:39 PM

@admin

can u give me more testcases ???

Hi admins,   Please through

vikaasgoel.007 @ 6 Feb 2010 12:53 AM

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

Shubhangini @ 21 Feb 2010 03:51 PM

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

Shubhangini @ 21 Feb 2010 03:51 PM

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

Shubhangini @ 21 Feb 2010 03:56 PM

also, i have tried other test cases.

are there any particularly tricky ones i should watch out for?

The first one you should have

triplem @ 21 Feb 2010 04:14 PM

The first one you should have tried was the largest possible one.. which immediately throws an exception.

i'm gettin the answer, on my

Shubhangini @ 22 Feb 2010 12:28 AM

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

x.xaby @ 30 Apr 2010 07:36 PM

#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

dabbcomputers @ 1 Jun 2010 12:23 AM

@everyone

is itoa function work on codecehf...........????????????? pls help.............

@Admin: I've tried my

manjotpahwa @ 2 Jun 2010 02:26 PM

@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

anubhav.7nov @ 20 Jul 2010 05:47 PM

@stephan

can u check why i am getting compilation error..

my code no. is 289809

@Stephen- Sir my solution

anurag11 @ 2 Aug 2010 11:20 PM

@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

gomsi_ @ 8 Aug 2010 06:12 PM

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

adityagupta @ 16 Dec 2010 12:04 PM

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

triplem @ 16 Dec 2010 12:57 PM

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

adityagupta @ 29 Dec 2010 06:19 PM

: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

triplem @ 30 Dec 2010 02:12 AM

Do you think that will be fast enough for the input I suggested?

my code is running fine on my

akanksha913 @ 26 Jan 2011 01:02 AM

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

triplem @ 26 Jan 2011 09:44 AM

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 @ 28 Jun 2011 08:50 PM
what can be the maximum value of the base ?

@nathanmelfoy: It can be

mukulgupta @ 29 Jun 2011 01:14 PM
@nathanmelfoy: It can be atmost "the maximum value of N - 1".

time limit exceede

sjs7007 @ 4 Sep 2011 10:29 AM
time limit exceede :( #include void convert(unsigned long int number,unsigned long int base,unsigned long int *converted,unsigned long int *index); void palcheck(unsigned long int converted[1000],unsigned long int *index,unsigned long int *test); main() { unsigned long int t,number,i,base,converted[1000],index,test=1,q; printf("Enter the number of test cases : "); scanf("%ld",&t); for(i=1;i<=t;i++) { printf("Enter number : "); scanf("%ld",&number); for(base=2;base<=number-1;base++) { convert(number,base,converted,&index); test=1; palcheck(converted,&index,&test); if(test==0) printf("%ld n",base) && (base=number+1); } q=number+1; if(test!=0) printf("%ld n",q); } } void convert(unsigned long int number,unsigned long int base,unsigned long int *converted,unsigned long int *index) { unsigned long int i,count; for(i=1;number>0;i++) { converted[i]=number%base; number=number/base; } count=i-1; *index=count; } void palcheck(unsigned long int *converted ,unsigned long int *index, unsigned long int *test) { unsigned long int reverse[*index+1],i,j,k=0; j=*index; for(i=1;i<=*index;i++) { reverse[i]=converted[j]; j--; } for(i=1;i<=*index;i++) { if (converted[i]==reverse[i]) k++; else (i=*index+1) && (*test=1); } if(k==*index) (*test=0); }

Try number 9999999145

soundblaster @ 20 Nov 2011 02:53 AM
Try number 9999999145

Every number is palindrome if

gautamjha @ 17 May 2012 05:49 PM
Every number is palindrome if base is 1 so and question is asking for samllest base so very simple answer is 1 always.

What is wrong with my code..I

ankit__ @ 24 May 2012 02:03 PM
What is wrong with my code..I am getting wrong answer..Pls help!!!! http://www.codechef.com/viewsolution/1057988

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

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.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com