Find least binary palindrome greater than a natural number
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
While it's not ready-to-use code, there are formulae given. Seems to me you'd have to implement A006995 and A206916. With that, A206914(n) = A006995(A206916(n)). In other words, in order to get the least palindrome larger than n (A206914(n)), you'd use A206916 to get the index into the list of all binary palindromes (A006995).
Attached is a list of the first 160E3 members of this sequence.
aa = readmatrix('b.txt');
daa = diff(aa,1,2); % distance to next palindrome
n = aa(:,1); % the argument
% plot the error (distance to next palindrome)
plot(n,daa); hold on
% just visually outline the boundary
sf = sqrt(9/2); % shape factor
plot(n,sf*sqrt(n)) % upper curve
plot(n,sf/2*sqrt(n)) % lower curve
% the boundary is found at the largest odd power of 2 < n
% the next palindrome should occur within [n n+nc]
p = floor((max(log2(n),1) + 1)/2)*2 - 1;
nc = 3*2.^((p - 1)/2);
plot(n,nc,'g')
From observation, it seems that the next palindrome occurs roughly within the interval [n+1 n+3*2.^((floor((max(log2(n),1) + 1)/2)*2 - 1 - 1)/2]. There's probably an actual formula for that.
Idk if any of this is useful information. I was just bored enough to poke at it.
Ken Bannister
on 10 Feb 2025
Image Analyst
on 10 Feb 2025
Edited: Image Analyst
on 11 Feb 2025
@Ken Bannister did you scroll down and see the other replies by other people?
Plus your link to oeis.org is broken, and you never answered my question if it's your homework (maybe because you didn't scroll down and only saw this one comment above.).
DGM
on 11 Feb 2025
Fixed the OEIS link. I noticed that yesterday, but forgot about it.
Sam Chak
on 11 Feb 2025
Hi @DGM, Thank you for the link. Are we permitted to submit @Voss–@Walter Roberson's heuristic solution in MATLAB for the binary palindromes problem to OEIS? I noticed that there are versions available in Maple, Mathematica, and Python.
Ken Bannister
on 11 Feb 2025
Ken Bannister
on 11 Feb 2025
Accepted Answer
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
Torsten
on 10 Feb 2025
What about the case given by the OP ? Original number 1 1 1 1 and least largest palindrome 1 0 0 0 1 ?
Walter Roberson
on 10 Feb 2025
1111
divide into
11 11
the two halves are equal so this is already a palindrome.
Add one to the first half producing
100
reflect producing
100 001
but as indicated there needs to be some touch-up to the rules for the case of odd lengths
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
on 10 Feb 2025
That's way simpler than I thought it would be based on the formulae given.
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
I think the OP means the least binary number greater than d that gives the same binary representation if read from left to right or if read from right to left. 16 wouldn't be such a number because
dec2bin(16)
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.
Categories
Find more on Variables 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!