# python – Natural numbers, in the binary notation of which units come in pairs

## Question:

We will consider natural numbers, in the binary notation of which units come in pairs. That is, next to each unit there is another unit, but no three units go in a row. For convenience, we will give such numbers a name. Let's say paired units . For example, the number 27 is paired with one, since its binary notation is as follows: 11011. And the number 47 is not suitable, since in the binary system it has three consecutive ones: 10 111 1. Here are the first few pairs of units: 3, 6, 12, 24, 27, 48, 51, 54, 96, 99, 102, 108, 192, 195, 198, …

So, we need to write a program that prints all pairs of unity numbers in a range specified by the user (checking for foolproofness can be neglected).

I got the following shvahkod (and even then after several unsuccessful attempts):

``````k1=int(input())
k2=int(input())
for j in range (k1, k2+1):
i=j
flag=1
while i!=0:
if i%2==0:
i=i//2
else:
i=i//2
if i==0:
flag=0
break
elif i!=0 and i%2==0:
flag=0
break
else:
i=i//2
if i%2==1:
flag=0
break
elif i!=0:
i//=2
if flag==1:
print(j)
``````

It seems to me that if I had studied at school, a computer science teacher would have given me for such “arts”, if not a deuce, then a triple for sure. In connection with the above, I am ready to accept any constructive comments. Both in improving the code and in the algorithm.

I wouldn't be too smart: https://ideone.com/tT7GT4

``````def check(x):
while x:
if (x&7) == 7: return False
if (x&3) == 1: return False
x >>= 1 + (x&1)
return True

print([x for x in range(1, 200) if check(x)])
``````
``````[3, 6, 12, 24, 27, 48, 51, 54, 96, 99, 102, 108, 192, 195, 198]
``````

I admit that it can be done more efficiently, but maybe it will go like that?

Scroll to Top