cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

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.

This page as a plain text file.
%I A327296 #58 Sep 25 2023 15:04:37
%S A327296 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,
%T A327296 21,27,27,31,1,33,33,21,33,21,21,51,33,21,21,51,21,51,51,31,33,21,21,
%U A327296 51,21,51,51,31,21,51,51,31,51,31,31,63,1,65,65,73,65,73
%N 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.
%C A327296 a(n) is the largest-valued binary palindrome that can be constructed from the binary representation of n. The palindrome may not have a leading 0.
%C A327296 When constructing a palindrome from a binary string, digits may only be permuted and deleted. I.e., the constructed palindrome will contain the same number or fewer 1's and 0's as the original binary string.
%C A327296 The algorithm is as follows:
%C A327296 If there is an odd number of 1's in the original binary string, start by placing a 1 in the middle of the output palindrome. Note that the remaining number of 1's is now even. If the remaining number of 1's is zero, then the constructed palindrome is simply "1" because the constructed palindrome cannot have leading zeros. If the remaining number of 1's is greater than zero, then place the remaining 0's evenly on either side of the previously placed "1". Because the output must be a palindrome, if the original string contains an odd number of 0's, then one of the 0's will be discarded. Lastly, place the remaining 1's on either end of the constructed palindrome.
%C A327296 If there is an even number of 1's in the original binary string, start by placing all of the 0's from the original string in the center of the palindrome, then place the 1's evenly on either side of the 0's.
%H A327296 Ian Band, <a href="/A327296/b327296.txt">Table of n, a(n) for n = 0..4094</a>
%e A327296 The largest palindrome that can be constructed:
%e A327296 For 101  it is  101, thus a(5)  = 5.
%e A327296 For 1000 it is    1, thus a(8)  = 1.
%e A327296 For 1010 it is 1001, thus a(10) = 9.
%e A327296 For 1011 it is  111, thus a(11) = 7.
%t A327296 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 *)
%o A327296 (Python)
%o A327296 def a(n):
%o A327296     #convert n to binary string
%o A327296     bin_str = str(bin(n))[2:]
%o A327296     #count 1's and 0's in the string
%o A327296     zeros = bin_str.count('0')
%o A327296     ones  = len(bin_str) - zeros
%o A327296     if(ones == 1 or ones == 0):
%o A327296         return ones
%o A327296     pal = ""
%o A327296     if(ones % 2 == 1):
%o A327296         #start by placing a '1' in the middle of the string
%o A327296         pal = '1'
%o A327296         #place as many 0's around the central '1' as possible
%o A327296         for i in range(0, zeros >> 1):
%o A327296                 pal = '0' + pal + '0'
%o A327296     else:
%o A327296         #place all 0's in the center
%o A327296         for i in range(0, zeros):
%o A327296             pal += '0'
%o A327296     #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
%o A327296     for i in range(0, ones >> 1):
%o A327296         pal = '1' + pal + '1'
%o A327296     #return integer value of the constructed palindrome
%o A327296     return int(pal, 2)
%Y A327296 Cf. A006995 (binary palindromes), A057148.
%K A327296 base,nonn,easy
%O A327296 0,4
%A A327296 _Ian Band_, Sep 16 2019
%E A327296 More terms from _Giovanni Resta_, Sep 16 2019