\$\begingroup\$Considering that the other question is a fastest-code code-challenge without an objective winning criterion (fastest measured how?), I propose closing the other and reopening this one.\$\endgroup\$
– DennisJul 3 '14 at 12:59

2

\$\begingroup\$I agree with @dennis and therefore I have upvoted the following unpopular answer on the original question. "If the answer you are looking for is strictly a fast result, then if you declare an array (of the gray codes)..." However the original question aready has a 7-character and a 4-character answer so I don´t see much room for improvement. Therefore I am not casting a reopen vote at present.\$\endgroup\$
– Level River StJul 3 '14 at 13:10

\$\begingroup\$The earliest Gray code question isn't great, but it already has answers which are fundamentally the same as the answers which this question wants, and which are not likely to be improved. I think it would have made more sense to leave this one closed and change the other one to a code golf.\$\endgroup\$
– Peter TaylorJul 3 '14 at 15:57

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 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:].

\$\begingroup\$Perfect example of how ù is used. Since N.z(n) is integer division with default arg = 2, you can save two bytes with 2pU Ç^z)¤ùTU: Try it online!\$\endgroup\$
– ETHproductionsJul 9 '18 at 18:31

\$\begingroup\$@ETHproductions Thanks, I still miss the default args every now and then. Very handy, thanks a lot.\$\endgroup\$
– NitJul 9 '18 at 18:43

Because the PowerShell I'm working with is only 2.0, I can't use any bit shifting cmdlets which might make for shorter code. So I took advantage of a different method described in the question source, flipping the array and adding it to itself, appending a 0 to the front of the top half, and a 1 to the bottom half.

F# (86)(84) (80)

for i in 0..(1<<<n)-1 do printfn"%s"(Convert.ToString(i^^^i/2,2).PadLeft(n,'0'))

This could probably be improved further.

Also note, if run in FSI, you'd need to open System;; first. If you'd like to avoid importing that, (and if you don't care about the order in which the values are printed) you can use this 82-character version:

for i in 0..(1<<<n)-1 do(for j in 0..n-1 do printf"%i"((i^^^i/2>>>j)%2));printfn""

W - compute 2^(input) (gets input implicitly):q - create range of numbers from 0 to 2^n - 1t - duplicate that range2/k - MATL doesn't have bitshift, so divide (each number) by 2 and floorZ~ - elementwise XOR that result with the original 0 to 2^n - 1 arrayB - convert each number in the result to binary
(Implicitly display output.)

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.