r/dailyprogrammer • u/Cosmologicon 2 3 • May 03 '21
[2021-05-03] Challenge #388 [Intermediate] Next palindrome
A palindrome is a whole number that's the same when read backward in base 10, such as 12321 or 9449.
Given a positive whole number, find the smallest palindrome greater than the given number.
nextpal(808) => 818
nextpal(999) => 1001
nextpal(2133) => 2222For large inputs, your solution must be much more efficient than incrementing and checking each subsequent number to see if it's a palindrome. Find nextpal(339) before posting your solution. Depending on your programming language, it should take a fraction of a second.
(This is a repost of Challenge #58 [intermediate], originally posted by u/oskar_s in May 2012.)
12
u/Nordellak May 03 '21 edited May 03 '21
I wrote the code in MATLAB:
The result is a vector containing the palindrome:
Edit: my code returned the same number if the inputted number was already a palindrome. Now it should find the next one.
11
u/[deleted] May 03 '21 edited May 03 '21
C
Maybe not the cleanest but I had a lot of fun writing it with my fiance, she figured out how to do the thing and I wrote the code.
Result:
Edit: formatting/cleanup
9
u/htsukebe May 03 '21 edited May 03 '21
Javascript
Results:
8
u/somebodddy May 03 '21
Rust
Results:
8
u/Gprime5 May 03 '21 edited May 06 '21
Python
Edit: Fixed the 9's overflow.
Output
11
u/touchstone1112 May 03 '21 edited May 04 '21
Python 3
Outputs
edit: reworked to catch cases noticed by u/Naratna . Thanks for checking my work, I hadn't considered all the cases.
4
u/madareklaw May 03 '21 edited May 09 '21
C#
Output
edit
Added another test
edit1
fixed bug where 998 would return 1001
Edit 2
Fixed console output
3
u/redundantimport May 03 '21
Thought I'd share some Crystal with y'all.
Running the code above yields
Last line of output is, as per the docs, user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds.
Cheers!
3
u/Tyaemiaes May 04 '21
Python solution without using string operations. 9,99,999,...-type numbers are handled as a special case, so not the most elegant.
2
u/Gylergin May 04 '21 edited May 04 '21
TI-Basic: The TI-84 that I run this on can only store 14 digits of a number. To find palindromes after numbers with more than 14 digits (like 339 ) you can utilize lists to store up to 999 digits. The algorithm this program uses is as follows:
Land the higher powerHL>H, increment the digit afterL, then make sure that digit is less than 10HintoLThe program will then return a number or a digit list, depending on what was entered.
Input:
808
999
2133
{4,0,5,2,5,5,5,1,5,3,0,1,8,9,7,6,2,6,7}
Output:
818
1001
2222
{4 0 5 2 5 5 5 1 5 3 5 1 5 5 5 2 5 0 4}
2
u/tlgsx May 04 '21 edited May 04 '21
Python 3.9, using /u/Gprime5 and /u/Nordellak clever approach
Output:
2
u/doubleunary May 04 '21 edited May 06 '21
Google Sheets spreadsheet formula using named ranges:
The same thing in one formula without using named ranges or helper columns:
Demo spreadsheet
2
u/BonnyAD9 May 05 '21 edited May 28 '21
C#, supports negative numbers (finds the first further from 0):
output:
edit:
fixed issue with 808
edit1:
code simplification
2
u/BonnyAD9 May 05 '21
Haskell solution:
Tests:
2
u/Marthurio May 27 '21
My attempt: https://github.com/maritims/next-palindrome
Heavily influenced by the solution from u/BonnyAD9 - I tried several things but in the end this is the only one that made sense. I learnt a lot about range operators, haven't bothered with those before!
1
u/skeeto -9 8 May 03 '21 edited May 03 '21
C using arbitrary precision over a string.
1
u/WrongHorseBatterySta May 04 '21 edited May 04 '21
Python 3:
28.6 ms ± 5.31 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
1
u/noble_liar17 May 04 '21
https://www.github.com/strikersps/Competitive-Programming/tree/master/SPOJ%2FPALIN%2Fthe_next_palindrome.c
1
u/Jayant0013 May 05 '21
Python 3 feedback apriciated it took about 24 ±4 sec to complete a list of 1000 random numbers ,its my first time posting here so please excuse any mistakes on my part
1
u/dirk_510 May 05 '21 edited May 05 '21
Java
This is my first solution submission. I'm not sure if I formatted it correctly, so I put the solution within spoiler tags.
I had to use the BigInteger class to handle the 339 input. I used a byte array to store and manipulate the digits. I'm not sure how efficient or elegant this solution is, but it works for all of the inputs I tried. I also tried to make the code as clean and readable as possible. Any feedback would be appreciated.
>!
!<
1
u/1516_Gang May 06 '21
Python:
Started learing the language yesterday, this is my Idea with the little knowledge i have of the language yet:
haven't looked at the other solutions till now, seems a little unconvinient compared to other solutions here lol.
1
u/Joshument May 06 '21 edited May 06 '21
I wrote this in Python:
Probably very messy, but I did it and I'm proud
1
u/KonjouHD May 06 '21 edited May 06 '21
Java, I tried to cut down on the nightmare spaghetti, but it's still pretty sprawling. It works though!
edit: missed a test print()
1
u/BonnyAD9 May 07 '21
My Python solution, it is quite unreadable so I added comments about what is happening:
Output:
1
u/BonnyAD9 May 07 '21 edited May 07 '21
C++ solution, works for arbitrarily large numbers if they are given in string:
Output:
edit: fixed issue with the large number
1
u/Lopsidation May 07 '21
Python 3. A simple and perverse method: rather than doing string manipulation on the target number, binary search over the set of all palindromes.
1
u/BonnyAD9 May 07 '21
Java:
Output:
1
u/gabyjunior 1 2 May 08 '21 edited May 09 '21
C, works for any number in base 2 to 36.
The base and number are read from the command line. The program converts the number from a string (removing the leading 0's) into an arbitry precision number (array containing the value of each digit) to find the next palindrome in the given base.
Output (run from bash)
1
u/BonnyAD9 May 08 '21
I did this in a windows Batch script, I had to create my own function for adding because Batch can handle only 32 bit signed integers:
Output:
1
u/warpjedi2 May 09 '21
C#
Results:
1
u/[deleted] May 09 '21
Typescript/Javascript: Two functions, one checks if the number is a palindrome, the other asks for the number and looks for both the higher and lower palindromes, compares them, and returns the closest. Very simple. First time doing this.
1
u/BonnyAD9 May 09 '21
JavaScript:
Results:
1
u/leftylink May 10 '21 edited May 18 '21
Ruby. With -v flag, verifies numbers up to N by comparing the fast algorithm vs the naive algorithm (iterate through numbers).
For example, verify up to 1 million:
To just calculate an answer, run without the v flag.
1
u/xelf May 13 '21 edited May 13 '21
python:
logic: convert to string, take the front half and reverse it to make the back half. While it's smaller than the original, increment the front half until higher than original. Watch for cases where we get a longer length (because we had to add an extra digit). Runtime is instant.
Runtime for all testcases I used is 0.001378 seconds.
1
u/Shhhh_Peaceful May 19 '21
Ugly Python
And the output:
1
u/CommunalSharpie May 21 '21
(finally) got a working TypeScript solution:
1
u/Habstinat May 22 '21 edited May 22 '21
javascript (not efficient)
1
u/jsun5192 May 23 '21 edited May 23 '21
Python Quickie!
Tester:
Result:
1
u/justchris5 May 23 '21
C#
1
u/joejr253 Jun 14 '21
This is what I came up with in Python3. Let me know what you guys think, I ran it 3 ** 30 and it took 2.76 seconds, I ran 3 ** 35 and it took 16.64 seconds. I'd like to get it to that fraction of a second like was mentioned. Thanks ahead of time.
1
u/Thin-State2086 Jun 14 '21 edited Jun 14 '21
#Python for a simple mind ;)
import math
def nextpal():
1
u/PoTAsh2000 Jun 22 '21
My solution in Java
Function:
Input:
Output:
1
u/I-Pop-Bubbles Jun 22 '21
Clojure - It's certainly not the prettiest, but here's my attempt at it.
Would love some feedback.
1
u/CunningBard1998 Aug 04 '21
1
u/ConsciousFinger2994 Aug 04 '21
python
1
u/_SetupWizard_ Aug 08 '21 edited Aug 08 '21
C# (For minimalists)
The answer to NextPal(3^39) is 4052555153515552504. My program finished in 0.0949 milliseconds.
1
u/loose_heron Aug 19 '21 edited Aug 19 '21
Python 3:
nextpal(3 ** 39) => 40525551535155525040.018 ms
1
u/simmons2714 Nov 14 '21 edited Nov 14 '21
Python #2 Attempt Best Version
Python #1 Attempt
#2 feels dirty and I hate myself for writing it. It feels like I cheated. I'm gonna have to revisit this problem later.
#1 pretty much stops working after 7 digits.
Also I just want to note that reddit's markdown is the worst and just entering code in general is awful. Why can't you just use regular markdown like the rest of the planet?
1
u/ironboy_ Apr 25 '23
JavaScript - short and efficient:
0
u/[deleted] May 05 '21
[deleted] — view deleted comment
0
u/[deleted] Aug 29 '21
[deleted] — view deleted comment