Two-Base Palindromes
October 21, 2014
I wanted to do this exercise as a follow-up to the earlier exercise on generating palindromes, but didn’t get around to it until now.
Your task is to write a program that generates a list of numbers that are palindromes in base 10 and base 8; for instance 149694110 = 55535558. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
A quick solution to perl (probably pipe the output through sort -n to get them fully in correct order)
In Java, pretty cheesy when using StringBuilder.
Here’s the list of dual-base palindromes I got when I let my program run overnight:
Java8 based on previous palindrom solution
Haskell: http://codepad.org/mTNnPb8l
Here is a better one in Java. Its uses two generators and works like “merging” them.
Generate the octal palindromes directly in binary and for each one check if we have one in base 10 as well.
About 10 mins to go up to 21 octal digits (we then run out of bits). The highest seems to be 322742453555354247223 so actually the second half of that time doesn’t find anything.
Here’s a Python version, so we aren’t limited to what will fit in a uint64_t.
Going a couple of hours now and up to 2650626939396260562 / 223107262414262701322.
After running for 4 days, my Python program has got well beyond 64 bit values and extended the list above with:
9843207767677023489 1042320625005260232401
32300361188116300323 3401017365665637101043
44643103022030134644 4656141232552321416564
77580854944945808577 10322464410401446422301
202693712161217396202 25763605624542650636752
371911868919868119173 50245203044744030254205
375183597404795381573 50532677140404177623505
375586160515061685573 50561160560606506116505
379080765242567080973 51063153270407235136015
473317002010200713374 63242276231313267224236
494635531909135536494 65501623271217232610556
552868393727393868255 73742270655755607224737
556362998454899263655 74244263365556336244247
579927810111018729975 76700353553235535300767
2503498805115088943052 417334063462264360433714
October 21st, 2014.c:
Output:
On an Apple Power Mac G4 (AGP Graphics) (450MHz processor, 1GB memory) to run the solution took approximately two seconds on Mac OS 9.2.2 (International English) (the solution interpreted using Leonardo IDE 3.4.1, where
UINT_MAX
is equal to65,535
).(I’m just trying to solve the problems posed by this ‘site whilst I try to get a job; I’m well aware that my solutions are far from the best – but, in my defence, I don’t have any traditional qualifications in computer science :/ )