Find least binary palindrome greater than a natural number
98 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Voss
2025 年 2 月 10 日 23:54
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));
その他の回答 (2 件)
Walter Roberson
2025 年 2 月 10 日 1:06
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 件のコメント
Sam Chak
2025 年 2 月 10 日 5:10
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)
DGM
2025 年 2 月 10 日 12:28
That's way simpler than I thought it would be based on the formulae given.
Image Analyst
2025 年 2 月 9 日 22:04
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 件のコメント
Image Analyst
2025 年 2 月 10 日 13:36
編集済み: Image Analyst
2025 年 2 月 10 日 13:37
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.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!