Find least binary palindrome greater than a natural number
4 views (last 30 days)
Show older comments
I am looking for an efficient way to generate the least binary number, b, that yields a natural number greater than a given natural number, d.
A small example would be d = 15, binary = 1111, and for which the least largest b = 10001 (decimal value 17).
Assume the natural number of interest, d, is 1 quadrillion (10^15) for which the binary representation, b, is:
'11100011010111111010100100110001101000000000000000'
I have tried one standard approach from the literature, which is to form the sum b + reverse(b) and check to see if the
result is a palindrome that yields a natural number that is just above d. So far, after many cycles, that hasn't produced anything.
There is no guarantee this method will always work, for example, it fails for the number 196.
There is a wealth of tantalizing clues in the "OEIS," the Online Encyclopedia of Integer Sequences"
7 Comments
Accepted Answer
Voss
on 10 Feb 2025
function out = get_next_binary_palindrome(d)
d_input = isnumeric(d);
if d_input
assert(isscalar(d));
b = dec2bin(d);
else % char assumed
assert(isrow(d));
b = d;
d = bin2dec(b);
end
n = numel(b);
m = rem(n,2);
idx = 1:(n+m)/2;
b_tmp = b(idx([1:end end-m:-1:1]));
d_tmp = bin2dec(b_tmp);
if d_tmp > d
if d_input
out = d_tmp;
else
out = b_tmp;
end
else
b_tmp = dec2bin(bin2dec(b(idx))+1);
if numel(b_tmp) > (n+m)/2
out = b_tmp([1:end-m end-1:-1:1]);
else
out = b_tmp([1:end end-m:-1:1]);
end
if d_input
out = bin2dec(out);
end
end
end
Examples:
get_next_binary_palindrome(15)
get_next_binary_palindrome('1111')
for d = 0:100
result = get_next_binary_palindrome(d);
fprintf('%d -> %d (%s)\n',d,result,dec2bin(result));
end
d = 1e15;
result = get_next_binary_palindrome(d);
fprintf('%d -> %d (%s)\n',d,result,dec2bin(result));
More Answers (2)
Walter Roberson
on 10 Feb 2025
There are two cases.
In the first case, the first half of the number is greater or equal to the reflected second half of the number. In such a case, the least palindrome is the first half of the number followed by its reflection.
For example
10110 00110
becomes
10110 01101
In the second case, the first half of the number is less than the reflected second half of the number. In such a case, the least palindorme is formed by adding 1 to the first half of the number (extending it if need be) and then reflecting that.
For example
10110 01110
becomes
10111 11101
These algorithms need to be touched up a bit if the length of the original number is odd
4 Comments
Sam Chak
on 10 Feb 2025
format longG
d = 15; % decimal system
b = dec2bin(d) % binary system
% length of bits
n = length(b); % may need to create rules for odd-numbered bits
% left-half of binary number
b1 = b(1:n/2)
% plus 1 (new left-half)
bL = dec2bin(bin2dec(b1) + 1)
% flip bits (right-half)
bL1 = bL(1:2) % need to create If-Else rules here
bR = fliplr(bL1)
% Concatenate bL and bR to form Palindromic bits
pb = [bL, bR]
% Convert back binary to decimal number
d3 = bin2dec(pb)
Image Analyst
on 9 Feb 2025
Can't you just do
b = dec2bin(d+1)
If not, then I'm not sure what you want. I'm not sure how 17 is the smallest natural number after 15.
2 Comments
Image Analyst
on 10 Feb 2025
Edited: Image Analyst
on 10 Feb 2025
OK thanks for clarifying for me. This sounds quirky enough to be a homework problem, at least I can't see any real world use case for it. @Ken Bannister is it your homework? And it seems like one way would be just a brute force for loop to check.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!