Skip to main content
3 of 5
forgot to update TIO link
Neil
  • 177.3k
  • 12
  • 74
  • 281

Charcoal, 30 29 bytes

NθNηIEXθηE﹪÷ιXθ⮌…⁰η⊗θ⌊⟦⁻⊖⊗θλλ

Try it online! Link is to verbose version of code. Outputs each digit of the gray code as a base 10 number on its own line, with each gray code double-spaced from the next. Explanation:

Nθ                              Input `N` as a number
  Nη                            Input `k` as a number
       θ                        Input `N`
      X                         To the power of
        η                       Input `k`
     E                          Map over implicit range
            ι                   Current value
           ÷                    Vectorised integer divide
              θ                 Input `N`
             X                  Vectorised to power
                …               Range from
                 ⁰              Literal integer `0`
                  η             To input `k`
               ⮌                Reversed
          ﹪                     Vectorised modulo
                    θ           Input `N`
                   ⊗            Doubled
         E                      Map over list
                          θ     Input `N`
                         ⊗      Doubled
                        ⊖       Decremented
                       ⁻        Subtract
                           λ    Current value
                     ⌊⟦         Minimum of that and
                            λ   Current value
          ⮌                     Reversed
    I                           Cast to string
                                Implicitly print

If omission of leading zeros is allowed then for 28 bytes:

NθNηIEEXθη↨ιθEι⎇﹪↨…ιμθ²⁻⊖θλλ

Try it online! Link is to verbose version of code. Explanation:

Nθ                              Input `N` as a number
  Nη                            Input `k` as a number
        θ                       Input `N`
       X                        To the power of
         η                      Input `k`
      E                         Map over implicit range
           ι                    Current value
          ↨                     Converted to base
            θ                   Input `N`
     E                          Map over base `N` values
              ι                 Current base `N` value
             E                  Map over base `N` digits
                  …ιμ           Current prefix
                 ↨              Convert from base
                     θ          Input `N`
                ﹪               Modulo
                      ²         Literal integer `2`
               ⎇                If odd then
                         θ      Input `N`
                        ⊖       Decremented
                       ⁻        Subtract
                          λ     Current digit
                           λ    Else current digit
    I                           Cast to string
                                Implicitly print

(Note that all leading zeros are omitted, so the first gray code is empty.)

Neil
  • 177.3k
  • 12
  • 74
  • 281