Python 2 (47)
for i in range(2**n):print bin(2**n+i/2^i)[3:]
The expression i/2^i
for the i
'th gray code number is from the this answerthis answer. To add leading zeroes that pad to length n
, I add 2**n
before converting to a binary string, creating a string of length n+1
. Then, I truncate the leading 1
and number type prefix 0b
with [3:]
.