Problem 61083. Express base-10 integers in lazy binary
The binary (or base-2) representations of a number n can be constructed as follows:
Step 1: If n = 0, then the binary representation is 0.
Step 2: If n > 0, then do the following:
a. Add 1 to the least-significant bit of the binary representation of n-1.
b. If any bit is equal to 2, replace that bit with 0 and add 1 to the next most significant bit.
c. Repeat step 2b till all bits are 0 or 1.
For example, the binary representation of 7 is 111. To get the binary representation of 8, we follow the steps to get 112, 120, 200, and the result 1000.
The procedure for determining the lazy binary representation is similar except that step 2c is omitted. That is, we are too lazy to repeat step 2b. Given that the lazy binary representation of 7 is 111, we would follow the modified procedure to get 112 and 120, the lazy binary representation of 8. For n = 9, the steps yield 121 and the result 201, and for n = 10, the steps yield 202 and the result 210.
Notice that in applying step 2b for constructing binary numbers, there will be at most one 2. However, in lazy binary, there could be multiple 2s. Change only the least significant 2, as in the calculation of the lazy binary representation of 10.
Write a function to compute the lazy binary representation of a base-10 integer. Express the result as a 64-bit unsigned integer.
Solution Stats
Problem Comments
-
2 Comments
Matthew
on 24 Nov 2025 at 22:18
Step 2b might need a bit of clarifying that you perform the step starting from the least significant bit. For the n=10 example, lazy binary for 9 is '201' so step 2a yields '202'. I initially interpreted step 2b such that it would yield '1010' and not '210'.
ChrisR
on 25 Nov 2025 at 4:57
Thanks Matthew. I added a note to the description.
Solution Comments
Show commentsProblem Recent Solvers5
Suggested Problems
-
Express base-10 integers in lazy binary
5 Solvers
More from this Author315
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!