Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 87 bytes

UsesBased on the recursive formula already pointed out by Parcly Taxel , but using a bitwise construction.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|2*(x>>k)+3*(y>>k&1)&3))

Try it online!

How?

Given \$n\$, we build a square matrix of width \2ドル^n\$:

[...Array(1 << n)].map((_, y, a) =>
 a.map(
 ...
 )
)

We fill this matrix with a recursive callback function. Because the content of the array we iterate over is not initialized, \$k\$ is undefined on the first call and set to the default value \$n\$. The second argument \$x\$, on the other hand, is well defined right away.

g = (k = n, x) => // k = counter, x = horizontal position
 k-- && // decrement k; if it was not 0:
 4 * g(k, x) | // append 4 times the result of a recursive call
 2 * (x >> k) + // set bit #1 if the k-th bit of x is set
 3 * (y >> k & 1) // toggle bits #0 and #1 if the k-th bit of y is set
 & 3 // ignore the other bits

JavaScript (ES6), 87 bytes

Uses the recursive formula already pointed out by Parcly Taxel.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|2*(x>>k)+3*(y>>k&1)&3))

Try it online!

JavaScript (ES6), 87 bytes

Based on the recursive formula already pointed out by Parcly Taxel , but using a bitwise construction.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|2*(x>>k)+3*(y>>k&1)&3))

Try it online!

How?

Given \$n\$, we build a square matrix of width \2ドル^n\$:

[...Array(1 << n)].map((_, y, a) =>
 a.map(
 ...
 )
)

We fill this matrix with a recursive callback function. Because the content of the array we iterate over is not initialized, \$k\$ is undefined on the first call and set to the default value \$n\$. The second argument \$x\$, on the other hand, is well defined right away.

g = (k = n, x) => // k = counter, x = horizontal position
 k-- && // decrement k; if it was not 0:
 4 * g(k, x) | // append 4 times the result of a recursive call
 2 * (x >> k) + // set bit #1 if the k-th bit of x is set
 3 * (y >> k & 1) // toggle bits #0 and #1 if the k-th bit of y is set
 & 3 // ignore the other bits
different method / same size
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 87 bytes

Uses the recursive formula already pointed out by Parcly Taxel.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|4-|2*(y>>k&1|2*x>>k&2x>>k)+3*(y>>k&1)&3))

Try it online! Try it online!

JavaScript (ES6), 87 bytes

Uses the recursive formula already pointed out by Parcly Taxel.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|4-(y>>k&1|2*x>>k&2)&3))

Try it online!

JavaScript (ES6), 87 bytes

Uses the recursive formula already pointed out by Parcly Taxel.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|2*(x>>k)+3*(y>>k&1)&3))

Try it online!

Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 87 bytes

Uses the recursive formula already pointed out by Parcly Taxel.

n=>[...Array(1<<n)].map((_,y,a)=>a.map(g=(k=n,x)=>k--&&4*g(k,x)|4-(y>>k&1|2*x>>k&2)&3))

Try it online!

AltStyle によって変換されたページ (->オリジナル) /