With this code, you can generate a list of binary numbers up to n in O (2 ^ int (log (n))) time complexity.
import math
num=25
n=int(math.log(num,2))
x=['0','1']
for i in range(1,int(n)+1):
for j in range(0,2**i):
x.append('1'+x[j])
x[j]='0'+x[j]
print(x[0:num])
output ::
['00000', '00001', '00010', '00011', '00100', '00101', '00110', '00111', '01000', '01001', '01010', '01011', '01100', '01101', '01110', '01111', '10000', '10001', '10010', '10011', '10100', '10101', '10110', '10111', '11000']
hope this helps
source
share