## 8/10 Palindromes

### July 10, 2018

Sometimes I enjoy browsing oeis.org (I might be a little bit weird). While browsing the other day, I found A029804, the sequence of numbers that are palindromes in both decimal and octal.

Your task is to write a program that writes the sequence of numbers that are palindromes in both decimal and octal. 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.

Advertisements

Interesting drill. Here is my solution of it, using Julia 0.6.2:

function IsPallindrome(x::Int64)

X = string(x)

n = length(X)

end

function dec2oct(x::Int64)

digit = mod(x, 8)

Z = string(digit)

x = div(x, 8)

end

function main(n::Int64)

Y = Array{Int64}(n)

c = 0

d = 0

end

Let’s test it:

Input: println(main(30))

Output: [0, 1, 2, 3, 4, 5, 6, 7, 9, 121, 292, 333, 373, 414, 585, 3663, 8778, 13131, 13331, 26462, 26662, 30103, 30303, 207702, 628826, 660066, 1496941, 1935391, 1970791, 4198914]

It would be interesting to see if there is any performance benefit by iterating over the Octal numbers first, or performing some hack. In any case, it’s too hot these days to do any more programming!

Cheers

In Python.

Mumps version

Even better is to generate both the stream of base-10 palindromes and the stream of base-8 palindromes and compare them:

A brute force Haskell version. You can supply any number of bases.

Here’s a solution in Python that works with arbitrary bases.

Example:

Here’s a slightly simpler version of

genpalsthat drops thecountdependency and loops directly overpowers.