Puzzle time - concatenary palindromes
-
OK, I turned my little 3-liner into a small back-tracking algorithm which stops early when a solution is no longer feasible, and it scales to n=19.
It turns out that there are 362,880 different palindromes one can make with 1..19.
And indeed the smallest one is the one Jon produced:
-
Interestingly, my algorithm can produce palindromes very quickly for up to n=22, but at n=23 there is suddenly a problem. The computer hasn't found anything in 10min or so. I assume the third "3" makes it impossible. Which raises the question: What is the set of numbers for which there are palindromes as described above. So far, we know that the
set includes the numbers 19 to 22 but not 2 to 18. -
-
SOLUTION: Suppose that our solution concatenates the numbers from 1 to N. Then each of the digits 1, 2, ..., 9, 0 must appear an even number of times in the numbers from 1 to N, with at most one exception, in order for those digits to be arrangeable into a palindrome.
So a natural first task is to determine the least N with this property, and it turns out to be 19. The numbers from 1 to 19 contain 12 1's, 2 of each digit from 2 to 9, and just one 0 (which would thus have to be in the middle of our concatenary palindrome).
We don't know yet whether there is a concatenary palindrome made from the numbers 1 through 19, but let's be optimistic and try to construct the smallest one we can. We'd want to start the number with the digit 1, and thus it must also end with 1. Can we begin it with two 1's? Yes, but only if we end it with the number eleven, and begin it with the number 1 followed by something in the teens. So our number looks like "1 1x ... 0 ... x 11" for some digit x. So far so good.
We can't have x = 1, but x = 2 is OK, so our number becomes "1 12 y ... 0 ... y 2 11" for some digit y. Can y be 1? No, because the y on the right side can only belong to the number one or the number eleven and we've already used those. But it can be 3, provided that on the right it's part of the number 13. So now we have "1 12 3 1z ... 0 ... z 13 2 11," where z is some digit — which can be taken to be 4.
Proceeding in this manner we end up with
1 12 3 14 5 16 7 18 9 10 19 8 17 6 15 4 13 2 11,
also known as 11,231,451,671,891,019,817,615,413,211, or "eleven octillion something."