A145799 a(n) = the largest integer that is an (odd) palindrome when represented in binary and that occurs in the binary representation of n.
1, 1, 3, 1, 5, 3, 7, 1, 9, 5, 5, 3, 5, 7, 15, 1, 17, 9, 9, 5, 21, 5, 7, 3, 9, 5, 27, 7, 7, 15, 31, 1, 33, 17, 17, 9, 9, 9, 9, 5, 9, 21, 21, 5, 45, 7, 15, 3, 17, 9, 51, 5, 21, 27, 27, 7, 9, 7, 27, 15, 15, 31, 63, 1, 65, 33, 33, 17, 17, 17, 17, 9, 73, 9, 9, 9, 9, 9, 15, 5, 17, 9, 9, 21, 85, 21, 21
Offset: 1
Examples
20 in binary is 10100. The largest binary palindrome included in this binary representation is 101, which is 5 in decimal. So a(20) = 5.
Links
- Reinhard Zumkeller, Table of n, a(n) for n = 1..16384
Programs
-
Haskell
a145799 = maximum . map (foldr (\b v -> 2 * v + b) 0) . filter (\bs -> bs == reverse bs && head bs == 1) . substr . bin where substr [] = [] substr us'@(_:us) = sub us' ++ substr us where sub [] = []; sub (v:vs) = [v] : [v : ws | ws <- sub vs ] bin 0 = []; bin n = b : bin n' where (n', b) = divMod n 2 -- Reinhard Zumkeller, Sep 24 2015
-
Mathematica
Block[{nn = 87, s}, s = Reverse@ Select[IntegerDigits[#, 2] & /@ Range[2^Log2@ nn], PalindromeQ]; Table[With[{d = IntegerDigits[n, 2]}, FromDigits[#, 2] &@ SelectFirst[s, SequenceCount[d, #] > 0 &]], {n, nn}]] (* Michael De Vlieger, Sep 23 2017 *)
Extensions
Extended by Ray Chandler, Oct 26 2008
Comments