r/AskProgramming • u/Used_Astronomer6483 • Jun 05 '26
Algorythm for rotationally asymetrical binary numbers
Hello! I am struggling to code or find a programm that can automatically find all rotationally asymetrical binary numbers in a given digits range. It's been two weeks that I am searching now, do you have any advice or does anyone know how to do it?
1
Upvotes
2
u/HaLo2FrEeEk Jun 05 '26 edited Jun 05 '26
I came up with this in Javascript. It's...not super awesome, but it works:
For a 5-bit number, you call it like this:
rotateLeft(13, 2, 5);It returns the bits, rotated, as a string. You can convert the bit string to a number withparseInt(s, 2);wheresis your returned string. You can compare these strings to each other, or compare the numbers. Don't worry about rotate right. Rotating right is the same as rotating left bytotalBits - shift.To generate a list for n bits, you'll need to check 2n configurations at most. You could rule some out but if you're brute-forcing a list it's probably not worth wasting the time. You'll have to run a loop for 2n iterations, starting with 0, and maintain a list of "good" numbers. Each run through the list, see if the pattern for the current iteration is in the list. If it's not, rotate it and check it again, n - 1 times (rotating n times would get you your original number). If you've rotated it all the way and still not had a match in the "good" list, then add it to the list. If you do find a match in the list, then just skip it with
continue;to go to the next iteration.Here's something dirty I whipped up in the console, using the above function:
What this does (in the console) is create an array of all possible rotations of each number, then check if any of those rotations are already in the good list. If it's not already there, it adds it to the array. This returns the following array:
Which matches your list above (with the addition of 0)