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
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » December 2009 (Contest XI) » Palindromic Numbers

Palindromic Numbers

Problem code: K2

  • 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


Date: 0000-00-00
Time limit: 3

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS


  • Submit

Comments

  • Login or Register to post a comment.

When i try to submit, it says

balajiganapath @ 1 Dec 2009 03:26 PM

When i try to submit, it says "contest_problem_id field is required.".

Please help

if it is not a palindrome thn

avix @ 1 Dec 2009 04:11 PM

if it is not a palindrome thn wht shuld be the answer?

there are no such numbers !

bsmanjunath @ 1 Dec 2009 04:21 PM

there are no such numbers ! all are palindromes with some base.

i m getting run time error

avix @ 1 Dec 2009 04:24 PM

i m getting run time error wht can be the problem it is workin well within the given limits?

mine, time limit exceeding !

bsmanjunath @ 1 Dec 2009 04:51 PM

mine, time limit exceeding !

can any1 put some more test

avix @ 1 Dec 2009 05:29 PM

can any1 put some more test cases?

Every number is palindromic

OMGTallMonster @ 1 Dec 2009 05:29 PM

Every number is palindromic in base 1!

Here is another test case.

Sarath @ 1 Dec 2009 06:19 PM
Here is another test case. This correct to my knowledge..
I am not responsible if this is wrong ;)

Input:

5
4567
23
656
23

Output:
21
3
3
3
9

I am sorry i missed a test

Sarath @ 1 Dec 2009 06:21 PM

I am sorry i missed a test case in input

 

Input:
5
4567
23
656
23
10000

Output:
21
3
3
3
9

can you put some bigger

avix @ 1 Dec 2009 06:33 PM

can you put some bigger values?my code is working correcly for the above cases

 

d output shud b like this

gauravgaba @ 1 Dec 2009 06:44 PM

d output shud b like this na

3

1

2

4

3

21

2

am i crct na pls tell

gauravgaba @ 1 Dec 2009 06:46 PM

am i crct na pls tell

admin some more cases some

avix @ 1 Dec 2009 06:58 PM

admin some more cases some big 1

Please do not post sample

admin @ 1 Dec 2009 07:05 PM

Please do not post sample test cases.

@aniruddha PLS tell for d

gauravgaba @ 1 Dec 2009 07:15 PM

@aniruddha PLS tell

for d case u hv given in the ques... d output shud b like this na

 

3

1

2

4

3

21

2

 

i.e one per line or entire output after tking all the input??????

 

PLS tell

@ Anirudda, sorry i din know

Sarath @ 1 Dec 2009 07:18 PM

@ Anirudda, sorry i din know thats forbidden

what is the output for input

shiplu @ 1 Dec 2009 07:21 PM

what is the output for input n = 10000000439 ?

mine is comin 2266 wht urs?

avix @ 1 Dec 2009 07:31 PM

mine is comin 2266 wht urs?

yes mine too, but actually i

shiplu @ 1 Dec 2009 07:44 PM

yes mine too, but actually i wanted to know n = 1000000439 ? it was my mistake in previous post to add a extra zero.

 

please tell me what is the output for this input ?

Guys, posting test cases and

imrankane2005 @ 1 Dec 2009 07:58 PM

Guys, posting test cases and asking for help for solving a problem is forbidden in running contest. You might be disqualified for this

http://blog.codechef.com/2009/07/29/rule-change-for-august-contest-guide...

 

1(7 0's)439 in a sec 2266 1(6

bsmanjunath @ 1 Dec 2009 08:11 PM

1(7 0's)439 in a sec 2266

1(6 0's)439 its taking lot of time for my code..

 

the time limit of 3 sec is

sesha_giri_nit @ 1 Dec 2009 10:11 PM

the time limit of 3 sec is for each test case or for all the test cases (limit of 1000)

Admin can you please answer

sesha_giri_nit @ 1 Dec 2009 10:35 PM

Admin can you please answer my question of time limit

@Giri For all 1000. @All :

admin @ 1 Dec 2009 10:49 PM

@Giri For all 1000.

@All : PLEASE DON'T POST TEST CASES. IT MIGHT LEAD TO DISQUALIFICATION.

but u din answer d question

virai @ 2 Dec 2009 12:50 AM

but u din answer d question dat al r palindroms for base 1.. n dats d smallest right!!

i have compelted my progam

hsn @ 2 Dec 2009 01:08 AM

i have compelted my progam and getting perfect output for whatever input i give still the compiler of codechef is saying wrong answer

can any body help me out

 

No.

triplem @ 2 Dec 2009 02:02 AM

No.

i am gettting runtime error

jitu_icfai @ 2 Dec 2009 09:00 AM

i am gettting runtime error in python.... but its runing fine on my system,

upto what base we need to

hsn @ 2 Dec 2009 09:39 AM

upto what base we need to compute.

will every input be a pallindrome for any of the base....

The problem statement clearly

triplem @ 2 Dec 2009 09:48 AM

The problem statement clearly explains this.

hello friends,i'm new to

suvro @ 2 Dec 2009 01:18 PM

hello friends,i'm new to codechef.this is the first time i'm participating in the challenge.can any one pls tell me if i need to give the inputs within the program?if not then as i'm using java ,so i need to know if the inputs will be given in this way 3 1 4 21,which is readable from command line.

@Aniruddha (Codechef): i am

jitu_icfai @ 2 Dec 2009 03:10 PM

@Aniruddha (Codechef):

i am gettting runtime error in python.... but its runing fine on my system,

@ALL, Codechef:

Why its giving runtime error for python, its happens for nov contest Also, But same code written in C is accepted in nov, But not in Python(Giving run time error)

@Jitendra Please check if you

admin @ 2 Dec 2009 04:34 PM

@Jitendra Please check if you are conforming to the I/O specifications mentioned. Take a look at some of the solutions in python for earlier contest problems to find out where you are going wrong.

@suvronil The input will be given from stdin and you have to write the output to stdout.

To Michael LeKander no number

Mohamed.Ganem @ 2 Dec 2009 05:17 PM

To Michael LeKander

no number could be represented in base 1 except 0

because all representations in base 1 is zero

 

Has the time limit been

hallinan @ 3 Dec 2009 02:49 AM

Has the time limit been lengthened? I notice in the successful submissions list , the user sppraveen with a time of 3.85 .

For solutions in java , its

sppraveen @ 3 Dec 2009 02:53 AM

For solutions in java , its time limit * 2

getting frustrated with

suvro @ 3 Dec 2009 03:21 AM

getting frustrated with runtime errors.but working fine on my machine.cant we see the errors occurring?otherwise it'll be difficult to solve the prob.

No, a good problem solver

triplem @ 3 Dec 2009 05:54 AM

No, a good problem solver should be able to and is expected to work that out for him/herself.

wat all bases do we hav to

adam @ 3 Dec 2009 10:58 AM

wat all bases do we hav to consider ?

Hello Friends,   I am new to

anilways @ 3 Dec 2009 02:19 PM

Hello Friends,

 

I am new to this forum, please help to undestand the the input.

Sorry i am not able to understand some of the basic terms. Please help me,

What is the input given below, can anyone suggest me to undestand it in a detailed,

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).

I have a general question.

kyun @ 3 Dec 2009 03:01 PM

I have a general question. When we say palindromes in any base, would that mean all the numbers in that base are to be treated as single digits? To clarify, would AA in hexadecimal (176 in base 10) be considered a palindrome in base 16?

I have a general question.

kyun @ 3 Dec 2009 03:02 PM

I have a general question. When we say palindromes in any base, would that mean all the numbers in that base are to be treated as single digits? To clarify, would AA in hexadecimal (176 in base 10) be considered a palindrome in base 16?

@Kyun Yes. @Anilkumar Please

admin @ 3 Dec 2009 10:03 PM

@Kyun Yes.

@Anilkumar Please go through our wiki at www.codechef.com/wiki. Please see solutions of problems which are open to all to understand what the input specifications mean.

 

what is the project format

balajir1990 @ 4 Dec 2009 12:10 PM

what is the project format for submitting multiclass java or jar files

time out error ...... huh??

shanky89 @ 4 Dec 2009 06:04 PM

time out error ...... huh??

I am getting the same error

anuragvickey @ 4 Dec 2009 10:54 PM

I am getting the same error "contest_problem_id field is required" time and again at the time of submission. May I know what the problem is?

Dear Admin, i am getting

vigneshnkl @ 5 Dec 2009 02:24 PM

Dear Admin,

i am getting TLE.

so my code is accepted ? only time limit exceeding ?

else my code also wrong.

dear admin,   one more

vigneshnkl @ 5 Dec 2009 02:26 PM

dear admin,

 

one more doubt..

do i have to take all input at one time and give outputs for all?

else i 'l get one by one simultaneously?

Read the FAQ. TLE means time

triplem @ 5 Dec 2009 02:43 PM

Read the FAQ.

TLE means time limit exceeded. It doesn't not say anything at all about whether your code is correct or not.

Thank u stephen.. also tell

vigneshnkl @ 5 Dec 2009 04:12 PM

Thank u stephen..

also tell me your suggestion for my second quest.

I did. I told you to read the

triplem @ 5 Dec 2009 04:22 PM

I did. I told you to read the FAQ.

@sarath 4567 base 21 is 1080.

notsoevil @ 5 Dec 2009 06:55 PM

@sarath 4567 base 21 is 1080. 4567 base 25 is 787.

how to inclue problem_id.

notsoevil @ 5 Dec 2009 07:07 PM

how to inclue problem_id. some one help plz

is no one here? everytime i

notsoevil @ 5 Dec 2009 07:56 PM

is no one here? everytime i submit i am told about contest_problem_id field. how do i get rid of it?

@Rajat Log out and log in

i0exception @ 6 Dec 2009 05:42 AM

@Rajat Log out and log in again.

continuing vaibhav shankar's

kvenkata @ 6 Dec 2009 01:30 PM

continuing vaibhav shankar's question, would one call 2424 to b a palindrome in base 100?

@k.venkata So you want to ask

iceman @ 6 Dec 2009 09:34 PM

@k.venkata

So you want to ask if your output is correct or not?

@deepankar no, i just want to

kvenkata @ 6 Dec 2009 11:10 PM

@deepankar

no, i just want to be sure with the definition of a palindrome in base b. i bliv 2424 is a pal. in base 100, and 4224 isnt, rt? im getting WA repeatedly, so just wanted to make sure that my definitions r rite....

how can we go to base more

ankurkkhurana @ 6 Dec 2009 11:32 PM

how can we go to base more than 36?

we have 10 digits and 26 alphabet........so how can we go to base more than 36?

i hav tried to submit the

Akhil Vinod @ 7 Dec 2009 02:19 AM

i hav tried to submit the solution to this problem many times now....but m gettin wrong answer...

 

so i was wondering if it is bcoz wen d input number is 0 or 1, the answer has to be 2.....right???

got it........sry

ankurkkhurana @ 7 Dec 2009 02:33 PM

got it........sry

Hello Admin What should be

pintu_agarwal @ 7 Dec 2009 07:40 PM

Hello Admin

What should be the value for the higest base here.???

Is it the number itself???

Please clarify...

For me the number 9991 is going beyond base 36 and still did not find any palindrome.

How should I represent a output for base more than 36. I have only this....

base[] = (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)

I have almost solved it.....just the few clarifiction will help me....before submitting....Plz reply

ok...I got the logic  

pintu_agarwal @ 7 Dec 2009 08:07 PM

ok...I got the logic

 

@ADMIN (CODECHEF) give us the

complanboy2 @ 7 Dec 2009 11:08 PM

@ADMIN (CODECHEF)

give us the test case file too.

so that we will work on it.

my c-programme runs good in my pc and generating correct o/p for the test cases given.but, the codechef compiler saying "wrong answer"

would you pls give the test cases where my code goes wrong?

i made solution in java..When

arunraja @ 8 Dec 2009 01:23 AM

i made solution in java..When i submit the system shows memory limit nearly 230,how shall i reduce it ??

note:I have imported some extra classes.

Is it not possible in java??

Hello Admin, I am getting

pintu_agarwal @ 8 Dec 2009 06:02 PM

Hello Admin,

I am getting this error when I try to submit.

Answers 236134 has been created.

Site Offline

The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.


If you are the maintainer of this site, please check your database settings in the settings.php file and ensure that your hosting provider's database server is running. For more help, see the handbook, or contact your hosting provider.

It could be a random

admin @ 8 Dec 2009 06:38 PM

It could be a random occurrence.

Thought I'd point this out to

tsaunat @ 9 Dec 2009 03:46 AM

Thought I'd point this out to people saying what if a number isn't a palindrome.   All numbers are a palindrome in the base number-1.   For example:   4678 is 11 (a palindrome) in base 4677

how to run this program in ur

peeyushagrawal @ 9 Dec 2009 08:46 PM

how to run this program in ur compiler

#include <stdio.h>

int main (void)

{

int n;

int *p;

scanf("%d",&n);

p=new int[n];

return 0;

}

 

i m getting

/sources/tested.c: In function 'main': /sources/tested.c:6: error: 'use' undeclared (first use in this function) /sources/tested.c:6: error: (Each undeclared identifier is reported only once /sources/tested.c:6: error: for each function it appears in.) /sources/tested.c:6: error: expected ';' before 'namespace' /sources/tested.c:10: error: 'new' undeclared (first use in this function) /sources/tested.c:10: error: expected ';' before 'int'

type of error

what i want is to make and dynamic array

so how to achieve this..


Huge primes are painful. Can

iceman @ 10 Dec 2009 04:21 PM

Huge primes are painful. Can anyone refer a good number theory book/site?

@peeyush: I guess you would

ruby @ 10 Dec 2009 04:46 PM

@peeyush:

I guess you would need to use a malloc()/calloc()/realoc() call to assign memory dynamically in C.

When will the solutions be

abhijith @ 11 Dec 2009 04:35 PM
When will the solutions be made public ?

When will the solutions

abhijith @ 11 Dec 2009 04:35 PM
When will the solutions become public ?

as the contest has ended can

shivmitra @ 11 Dec 2009 05:23 PM

as the contest has ended can anyone tell what was the logic behind the prob???i mean which property of the palindroms to b used here???

There is no particular

admin @ 11 Dec 2009 05:37 PM

There is no particular properly of a palindrome. Just think of what it means to represent a number in a particular base. If you fix up the last digit for a number in a particular base, how would that affect the palindromic nature with the given constraints.

Folks might want to look up

ContestCrackers @ 11 Dec 2009 06:39 PM

Folks might want to look up the following URLs to get an idea of one approach to solving this problem.

http://en.wikipedia.org/wiki/Strictly_non-palindromic_number

http://www.mathpages.com/home/kmath359.htm

http://www.research.att.com/~njas/sequences/A016038

This approach would result in an O(n^0.5) algorithm.

I am new to Code Chef.. Where

betamoo @ 14 Dec 2009 03:15 AM

I am new to Code Chef.. Where can I submit my solution

 

The problems have been moved

triplem @ 14 Dec 2009 03:29 AM

The problems have been moved to the practice section; you can submit this one at http://www.codechef.com/problems/K2/ (just get rid of DEC09 from the URL).

can anybody tell me what is

bigbang4u2 @ 19 Dec 2009 02:14 AM

can anybody tell me what is wrong with this solution

I am getting all ans correct but it says wrong ans on codechef

 

#include<stdio.h>

#include<iostream>

#include<string.h>

using namespace std;

string base_cal(long int num, long int base)

{

string str;

long int div;

int rem;

char ch;

while(num>1)

{

rem=num%base;

ch = (char)rem;

 

str.append(1,ch);

 

num=num/base;

 

if(num==1)

str.append(1,1);

 

}

 

return str;

}

 

int pallindrome(string s)

{

string str1;

int i;

 

for(i=s.length()-1;i>=0;i--)

{

 

str1.append(1,s.at(i));

}

 

 

if(str1==s)

return 1;

else

return 0;

 

}

 

 

 

int main()

{

 

int i,t;

long int num[1000], j;

cin>>t;

string s;

int check,val=2;

for(i=0;i<t;i++)

{

cin>>num[i];

if(num[i]==1)

cout<<val<<endl;

for(j=2;j<num[i];j++)

{

 

s = base_cal(num[i],j);

 

check = pallindrome(s);

 

if(check==1)

{

cout<<j<<endl;

break;

}

}

}

return 0;

}

#include<stdio.h>#include<ios

sumiucoe@live.com @ 23 Dec 2009 06:24 PM

#include<stdio.h>
#include<iostream.h>
#include<string.h>
string base_cal(long int num, long int base)
{
string str;
long int div;
int rem;
char ch;
while(num>1)
{
rem=num%base;
ch = (char)rem;
str.append(1,ch);
num=num/base;
if(num==1)

str.append(1,1);
}
return str;
}
int pallindrome(string s)
{
string str1;
int i;
for(i=s.length()-1;i>=0;i--)
{
str1.append(1,s.at(i));

}

if(str1==s)
return 1;
else
return 0;
}
int main()
{
int i,t;
long int num[1000], j;
cin>>t;
string s;
int check,val=2;
for(i=0;i<t;i++)
{
cin>>num[i];
if(num[i]==1)
cout<<val<<endl;
for(j=2;j<num[i];j++)
{
s = base_cal(num[i],j);
check = pallindrome(s);
if(check==1)
{
cout<<j<<endl;
break;
}
}
}
return 0;
}

  Hi Shivam, are you sure

sumiucoe@live.com @ 23 Dec 2009 06:26 PM

 

Hi Shivam,

are you sure this code produce correct result..

i didnt think so..

I programmed it to success.

madhavcsit @ 29 Dec 2009 03:03 AM

I programmed it to success. But i dont know how to upload can u plz help me?

sorry probably this might be

madhavcsit @ 29 Dec 2009 03:15 AM

sorry probably this might be a closed one.. plz let me know if it is still open..

This was a contest problem.

triplem @ 29 Dec 2009 04:04 AM

This was a contest problem. The practice problem is at http://www.codechef.com/problems/K2/

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
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 computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various 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 judge accepts solutions in over 35+ programming languages. Online programming was 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 competitions 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 programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

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. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

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