| Bytes | Lang | Time | Link |
|---|---|---|---|
| 065 | JavaScript Node.js | 230805T164111Z | l4m2 |
| 066 | Python | 230727T054917Z | xnor |
| 068 | Wolfram LanguageMathematica | 230730T092512Z | 138 Aspe |
| 054 | Ruby | 230726T225110Z | Level Ri |
| 053 | R | 230726T165522Z | Giuseppe |
| 022 | BQN CBQN | 230726T205940Z | LeopardS |
| 825 | Vyxal | 230726T133052Z | lyxal |
| 010 | Thunno 2 | 230726T141620Z | The Thon |
| 050 | Arturo | 230727T020554Z | chunes |
| 051 | PARI/GP | 230727T013833Z | alephalp |
| 014 | Pyth | 230726T142946Z | CursorCo |
| 018 | Charcoal | 230726T184146Z | Neil |
| 008 | Jelly | 230726T180640Z | Lynn |
| 010 | Jelly | 230726T174513Z | caird co |
| 074 | JavaScript ES6 | 230726T173116Z | Arnauld |
| 052 | Raku | 230726T155532Z | Sean |
| 119 | Nim | 230726T151943Z | xigoi |
| 011 | Nibbles | 230726T150019Z | xigoi |
| 010 | 05AB1E | 230726T143630Z | Kevin Cr |
| 015 | 05AB1E | 230726T142440Z | The Thon |
| 057 | Factor + math.matrices math.unicode | 230726T141310Z | chunes |
| 072 | Python | 230726T133907Z | mousetai |
Python, 66 bytes
lambda m:(q:=range(2**m))and[[(n:=i&j)>n^n-1for i in q]for j in q]
mousetail's answer, but with bit operations instead of bit_count. We want to check that i&j (which we call n) doesn't have exactly one set bit, which means that it's a not power of two. We do this by checking that n>n^n-1, which is processed as n>n^(n-1). Let's check that this works in every case:
- If
nis a power of two, thenn-1has disjoint bits set tonand so their XOR is greater thann. - If
nis any other positive value, thennandn-1share the same leading bit, and the XOR cancels this bit making the result smaller thann. - For
n==0, the result is0, the same asn.
Annoyingly Python doesn't let us do the walrus assignment q:=range(2**m) in the iterable of a list comprehension, so the code has it assigned beforehand. loopy walt points out a better way to handle the two levels of iteration is using the classic divmod two-loop trick together with the classic zip/iter chunker.
61 bytes
lambda m:zip(*2**m*[((n:=j&j>>m)>n^n-1for j in range(4**m))])
Wolfram Language(Mathematica), 68 bytes
Golfed version. Try it online!
f@n_:=Array[Boole[1!=Tr@IntegerDigits[BitAnd[#-1,#2-1],2]]&,2^{n,n}]
Ungolfed version. Try it onlinne!
f@n_:=Table[If[Total[IntegerDigits[BitAnd[i-1,j-1],2]]!=1,1,0],{i,1,2^n},{j,1,2^n}]
Ruby, 72 56 54 bytes
->n{(w=0...1<<n).map{|i|w.map{|j|a=i&j;(a&a-1)**a>0}}}
This employs one of my favourite bit-twiddling hacks. a&a-1 gives 0 if a has only one bit set. Unfortunately it also gives 0 if a==0 so we use the fact that any number (including 0) raised to the power 0 is 1 to catch this special case: (a&a-1)**a.
So now we have distinction between nonzero and zero numbers. The rules require two distinct values, so we use >0 to convert to true/false and we are done.
The footer in the linked TIO formats the output 2d array line by line, and converts the true/false to 1/0.
R, 55 53 bytes
function(n)(y=outer(x<-1:2^n-1,x,bitwAnd))^0-y%in%2^x
Rather than counting the number of on bits (which is very lengthy in R), tests the equivalent characterization of "equal to a power of 2"; then has to do some reshaping of the vector output of %in% to get back to the right shape matrix.
BQN (CBQN), 45 bytes 22 ‘bytes’
{1≠+´¨∧⌜˜⥊∾⌜⍟(𝕩-1)˜↕2}
I think the following is slightly more elegant, and it's the same number of characters but, alas, more bytes.
{1≠+´∘∧⌜˜⥊(↕2)∾⌜⍟𝕩⋈⟨⟩}
We operate only on bit-strings, rather than integers. ⥊⌜⍟(𝕩-1)˜↕2 gives us the list of binary strings of length 𝕩, which is then anded into a table, and summed: +´∘∧⌜˜. We then take only the 1≠ entries.
Vyxal, 66 bitsv2, 8.25 bytes
Eʁ:v⋏bvṠċ
-4.75 bytes thanks to TheThonnu
The footer formats the resulting matrix into a nice grid like the test cases. Remove it for just a list of lists. Outputs inverted numbers (0s for 1s and 1s for 0s).
I somehow managed to generate like 3 other fractals while solving this, including an upside down triangle thing with the bottom off-center.
Explained
EDẊ‹sƛƒ⋏bT₃;ẇ
ED # Push three copies of n ** 2
Ẋ # Cartesian product of the range [1, n ** 2] and [1, n ** 2]
‹ # with all sublists decremented
s # and sorted
ƛ ; # To each pair
ƒ⋏ # reduce by bitwise and
bT₃ # is the length of truthy indices 1?
ẇ # wrap into 2 ** n chunks
💎
Created with the help of Luminespire.
Thunno 2, 10 bytes
2RẉDȷỌ€ʂ⁻Q
Outputs a nested list. The footer formats it into the grid. Port of Lynn's Jelly answer.
Explanation
2RẉDȷỌ€ʂ⁻Q # Implicit input
2Rẉ # [1,2] to the cartesian power of the input
DȷỌ # Outer product over minimum with itself
€ʂ # Sum each inner list
⁻Q # Decrement, not equal to the input?
# Implicit output
Old:
OLDȷÆ&ıḃ1ȷcḅ # Implicit input
OL # Push [0..2**n)
DȷÆ& # Outer product over bitwise AND
ıḃ1ȷc # Popcount of each
ḅ # Equals one?
# Implicit output
Arturo, 57 54 50 bytes
$->n[map^2n'x->map^2n=>[0<>^and dec<=<=and&-1x-1]]
$->n[ ; a function taking an argument n
map^2n'x-> ; map over [1..2^n]; assign current elt to x
map^2n=>[ ; map over [1..2^n]; assign current elt to &
0<> ; is zero not equal to
and&-1x-1 ; bitwise and of current elts minus one
<=<= ; duplicated twice
dec ; minus one
and ; bitwise and
^ ; NOS to the TOS power
] ; end map
] ; end function
Pyth, 15 14 bytes
mmn1s&VdkQ=^U2
Explanation
mmn1s&VdkQ=^U2Q # implicitly add Q
# implicitly assign Q = eval(input())
=^U2Q # Q = repeated cartesian product of [0,1] Q times
m Q # map lambda d over range(Q)
m Q # map lambda k over range(Q)
&Vdk # d & k, vectorized
s # sum bits
n1 # != 1
Charcoal, 18 bytes
≔X²NθEθ⭆θ¬⁼¹Σ↨&ιλ²
Try it online! Link is to verbose version of code. Explanation:
² Literal integer `2`
X Raised to power
N Input as a number
≔ θ Save in variable
θ Saved variable
E Map over implicit range
θ Saved variable
⭆ Map over implicit range and join
ι Outer value
& Bitwise And
λ Inner value
↨ ² Convert to base `2`
Σ Take the sum
¬⁼ Does not equal
¹ Literal integer `1`
Implicitly print
Jelly, 8 bytes
2ṗ«'`§’n
(input: n)
2ṗ nth Cartesian power of [1, 2]: for example,
[[1,1,1,1], [1,1,1,2], [1,1,2,1], ..., [2,2,2,2]] if n=4
«'` self-table by vectorized minimum
§ sum each vector
’ subtract 1
n not equal to… (implicit argument: n)
Instead of 0b0011 & 0b0110 = 0b0010 we do 1,1,2,2 « 1,2,2,1 = 1,1,2,1.
Instead of checking popcount ≠ 1, we check sum ≠ n+1. §’n is shorter than ’§n1.
Jelly, 10 bytes
2*Ḷ&þ`B§n1
How it works
2*Ḷ&þ`B§n1 - Main link. Takes n on the left
2* - 2 ** n
Ḷ - [0, 1, ..., 2**n)
þ` - To every pair (a, b) in this range:
& - Bitwise and
B - Convert all to binary
§ - Sums
n1 - Does not equal 1?
JavaScript (ES6), 74 bytes
n=>[...Array(1<<n)].map((_,y,a)=>a.map((_,x)=>(g=k=>k?k%2^g(k/2):1)(x&y)))
Raku, 52 bytes
{(^$_ X+&^$_).rotor($_)».&{!$_||$_!=1+<.msb}}o 1+<*
The version of Raku on TIO fails to parse the !$_||$_!=1+<.msb bit, presumably due to a bug, so I've expressed it there as !($_&&$_==1+<.msb), which is equivalent by De Morgan's law, but two bytes longer.
This is an anonymous function consisting of two separate anonymous functions composed together with o. The right-hand function is 1 +< *, which shifts the number 1 leftwards a number of bits given by its argument; that is, two to the power of the argument. That number is passed to the left-hand function, where the variable $_ gets its value.
^$_is a range of numbers from 0 up to one less than$_. For example, when the number passed to the first function is 3, this is the range0..7.X+&gives the cross product of one copy of that range with another using the bitwise-and operator+&..rotor($_)takes that flat crossed list and makes it into a square matrix.».&{ ... }recursively applies the code between the braces to each element of that matrix.!$_ || $_ != 1 +< .msbtests whether each number is zero (!$_) or consists of only a single binary digit by comparing the number to 1 bit-shifted left a number of places equal to the number's most significant bit (1 +< .msb).
Nim, 119 bytes
import bitops
proc f(n:int)=
let r=0..<1 shl n;for i in r:
var s="";for j in r:s&= $int 1!=popcount i and j
echo s
Nibbles, 11 bytes
.;`,^2$.@!=1+``@`&@$
Returns a matrix with truthy values (positive numbers) for 1 and falsy values (0) for 0.
05AB1E, 10 bytes
o<ÝDδ&b1ö≠
Outputs as a matrix with 1s and 0s.
Port of @mousetail's Python answer, so make sure to upvote that answer as well!
Try it online or verify the first 5 test cases.
Explanation:
o # Push 2 to the power the (implicit) input-list
<Ý # Pop and push a list in the range [0,2**input)
Dδ& # Create a bitwise-AND table of it:
D # Duplicate the list
δ # Pop both lists and apply double-vectorized:
& # Bitwise-AND
b # Then convert each inner value to a binary-string
1ö # Vectorized-sum its digits by converting from base-1 to a base-10 integer
≠ # Check for each that it's NOT equal to 1 (0 if 1; 1 otherwise)
# (after which the matrix is output implicitly)
05AB1E, 15 bytes
oDL<ãε`&b1¢Θ}sô
Outputs a nested list. The footer formats it into the grid. Port of lyxal's Vyxal answer.
-1 thanks to @KevinCruijssen
Explanation
oDL<ãε`&b1¢Θ}sô # Implicit input
oD # Push 2 ** n and duplicate
L<ã # Cartesian product of [0..2**n) with itself
ε } # Map over this list:
`& # Bitwise AND of the pair
b1¢Θ # Is the popcount equal to 1?
sô # Split into 2 ** n chunks
# Implicit output
Factor + math.matrices math.unicode, 57 bytes
[ 2^ dup [ bitand bit-count 1 ≠ ] <matrix-by-indices> ]
2^ ! two to the input power
dup ! duplicate
[ ... ] <matrix-by-indices> ! create an MxN matrix with indices on top of stack
bitand ! bitwise and
bit-count ! number of on bits
1 ≠ ! not equal to one
Python, 72 bytes
lambda m:(q:=range(2**m))and(((i&j).bit_count()!=1for i in q)for j in q)