19
2
inspired by Count down from infinity
Given a non-negative integer N
, output the number of repetitions of the following steps it takes to reach 0:
- Convert
N
to binary (4812390 -> 10010010110111001100110
) - Flip each bit (
10010010110111001100110 -> 01101101001000110011001
) - Trim leading zeroes (
01101101001000110011001 -> 1101101001000110011001
) - Convert back to decimal (
1101101001000110011001 -> 3576217
)
Rules
- Input and output may be in any unambiguous, consistent format
- The input will be within the native representable integer range for your language (if your language supports arbitrarily-large integers, there is no bound)
Test Cases
0 -> 0
1 -> 1
42 -> 6
97 -> 3
170 -> 8
255 -> 1
682 -> 10
8675309 -> 11
4812390 -> 14
178956970 -> 28
2863311530 -> 32
This sequence is A005811 in the OEIS.
8Step 3 is of no use at all – edc65 – 2016-11-03T07:29:40.567
@edc65 Seems like you could do either step 3 or step 4, depending on how your algorithm is laid out – Brian J – 2016-11-03T13:42:00.243
@edc65 Maybe for you it is of no use. A simple inverse operator doesn't trim leading zeros for you.
~(~a) == a
– Poke – 2016-11-03T13:42:23.2701@Poke Bitwise NOT inverts all bits of the binary representation, including leading zeroes (and an infinite amount of them in languages with arbitrary precision integers). This is not equivalent to step 2. – Dennis – 2016-11-03T15:26:13.517
@Poke A simple inverse operation is different from applying the steps 1..4. If you want to apply these steps, the step 3 is of no use, because the flip in step 2 (as it is shown) does not change the leading 0s. If the step 2 does change the leading 0s to leading 1s, then obviuosly you have to remove the leading 1s in step 3, not the leading 0s – edc65 – 2016-11-03T16:20:31.387
Got it. I still wouldn't say that step 3 is of no use. It's more that the steps seem inconsistent with the examples for each step. – Poke – 2016-11-03T17:17:54.850