r/dailyprogrammer • u/oskar_s • May 28 '12
[5/28/2012] Challenge #58 [intermediate]
For the easy part of today's challenge, we considered numbers that are palindromes in different bases. For this problem, lets only concern ourselves with numbers that are palindromes in base 10.
Define a function P(N) that takes as input a number N, and returns the smallest base 10 palindrome larger than N (i.e. it returns the "next" palindrome after N). So, for instance:
P(808) = 818
P(999) = 1001
P(2133) = 2222
What is P( 339 )?
BONUS: What is P( 7100 )
- Thanks to ashashwat for suggesting this problem at /r/dailyprogrammer_ideas! (problem originally from here) If you have a problem that you think would be good for this subreddit, why not head over there and suggest it?
6
Upvotes
2
u/JerMenKoO 0 0 May 28 '12
Merely the same: http://www.spoj.pl/problems/PALIN/
4
u/Taur1ne May 29 '12
I went into this program thinking that it was pretty simple. I brute forced it and just incremented the input by 1 until it came across a palindrome. The first three examples ran with ease. When I tried running the problem of 339 I found out how inefficient my code was.
1
u/[deleted] May 30 '12
I tried to solve this one, but I'm having a hard time finding a data type in C# that will handle a number the size of 7100 with enough precision. It seems that the number is larger than a long or ulong can handle and a double doesn't store enough significant digits.
2
u/ProofByPicture May 31 '12
executed in 3.8e-5s, python.
Answer:
1
u/robotfarts May 28 '12
1
u/xjtian May 28 '12 edited May 29 '12
This one was fun...
Python:
Results:
EDIT: Fixed!
1
u/exor674 May 28 '12 edited May 28 '12
I hate to not put this in a comment but it is 260 lines hah: http://pastie.org/private/mqwedq1pzx8kpny6jtzpa
[ this comment refers to http://pastie.org/private/x5oygu6ankx0u8ca942uwa ] -- yes, I was being silly!
1
u/luxgladius 0 0 May 29 '12
Perl
Output
808: 818
999: 1001
2133: 2222
4052555153018976267: 4052555153515552504
3234476509624757991344647769100216810857203198904625400933895331391691459636928060001: 3234476509624757991344647769100216810857204027580186120019677464431997574269056744323
1
u/Arthree May 29 '12 edited May 31 '12
Autohotkey_L:
Not sure how to do the bonus without writing a function to manually multiply numbers and put the running total into a string, since AHK has no BigInt functionality :(
1
u/herpderpdoo May 29 '12 edited May 30 '12
I have the nagging suspicion I'm wrong, but I get all the right answers in almost no time. Can someone check me?
Python code:
output: