Explicit formula for Nth string of Gray Code.

473 Views Asked by At

From Wolfram MathWorld, we have:

"A Gray code is an encoding of numbers so that adjacent numbers have a single digit differing by 1. The term Gray code is often used to refer to a "reflected" code, or more specifically still, the binary reflected Gray code (...). The code is called reflected because it can be generated in the following manner. Take the Gray code 0, 1. Write it forwards, then backwards: 0, 1, 1, 0. Then prepend 0s to the first half and 1s to the second half: 00, 01, 11, 10. Continuing, write 00, 01, 11, 10, 10, 11, 01, 00 to obtain: 000, 001, 011, 010, 110, 111, 101, 100, ..."

Therefore, to obtain the n-th string, must know all the previous strings. Is there a mathematical formula that can return the nth string without knowing the first n-1 strings?

Thank you very much.

1

There are 1 best solutions below

0
On BEST ANSWER

The same MathWorld article you link has

To convert a binary number $d_1d_2\ldots d_{n-1}d_n$ to its corresponding binary reflected Gray code, start at the right with the digit $d_n$ (the $n$th, or last, digit). If the $d_{n-1}$ is $1$, replace $d_n$ by $1-d_n$; otherwise, leave it unchanged. Then proceed to $d_{n-1}$. Continue up to the first digit $d_1$, which is kept the same since $d_0$ is assumed to be a $0$. The resulting number $g_1g_2\ldots g_{n-1}g_n$ is the reflected binary Gray code.