A327296 a(n) is the decimal value of the largest binary palindrome that can be obtained from the digits of the binary representation of n.
0, 1, 1, 3, 1, 5, 5, 7, 1, 9, 9, 7, 9, 7, 7, 15, 1, 17, 17, 21, 17, 21, 21, 27, 17, 21, 21, 27, 21, 27, 27, 31, 1, 33, 33, 21, 33, 21, 21, 51, 33, 21, 21, 51, 21, 51, 51, 31, 33, 21, 21, 51, 21, 51, 51, 31, 21, 51, 51, 31, 51, 31, 31, 63, 1, 65, 65, 73, 65, 73
Offset: 0
Examples
The largest palindrome that can be constructed: For 101 it is 101, thus a(5) = 5. For 1000 it is 1, thus a(8) = 1. For 1010 it is 1001, thus a(10) = 9. For 1011 it is 111, thus a(11) = 7.
Links
- Ian Band, Table of n, a(n) for n = 0..4094
Programs
-
Mathematica
a[n_] := Block[{o,z}, {o,z} = DigitCount[n, 2]; If[o==1, 1, If[OddQ@ o, {o,z} = Floor[{o,z} /2]; 2^(z + o) + (2^o - 1) (1 + 2^(o + 1 + 2 z)), o = o/2; (2^o - 1) (1 + 2^(o + z))]]]; a /@ Range[0, 70] (* Giovanni Resta, Sep 16 2019 *)
-
Python
def a(n): #convert n to binary string bin_str = str(bin(n))[2:] #count 1's and 0's in the string zeros = bin_str.count('0') ones = len(bin_str) - zeros if(ones == 1 or ones == 0): return ones pal = "" if(ones % 2 == 1): #start by placing a '1' in the middle of the string pal = '1' #place as many 0's around the central '1' as possible for i in range(0, zeros >> 1): pal = '0' + pal + '0' else: #place all 0's in the center for i in range(0, zeros): pal += '0' #place the remaining 1's (guaranteed to be an even number, because one '1' was placed in the middle) on either side of the palindrome for i in range(0, ones >> 1): pal = '1' + pal + '1' #return integer value of the constructed palindrome return int(pal, 2)
Extensions
More terms from Giovanni Resta, Sep 16 2019
Comments