Answer the question
In order to leave comments, you need to log in
Problem with replacing list item?
Given the conditions of the problem:
The algorithm receives a natural number N > 1 as input and builds a new number R from it as follows:
1. A binary representation of the number N is constructed.
2. Instead of the last (rightmost) binary digit, the second digit of the binary representation from the left is written twice.
3. The result is converted to decimal.
Example. The number N = 19 is given. The algorithm works as follows:
1. Binary notation of the number N: 10011.
2. The second digit from the left is 0, the unit at the end of the entry is replaced by two zeros, the new entry is 100100.
3. The result of the algorithm is R = 36.
With What is the smallest number N that will result in R > 76 as a result of the algorithm? Write your answer in decimal notation.
for n in range(1, 1000):
r = [str(bin(n))]
r[-1] = r[3] * 2
while int(r, 2) > 76:
n += 1
print(int(r, 2))
Answer the question
In order to leave comments, you need to log in
Well,
binary = bin(19)
int(binary[:-1] + binary[3] * 2, 2) # 36
N = 19
L = N.bit_length() - 2
(N >> 1 << 2) | 0b11 * ((N & 1 << L) >> L) # 36
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question