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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Compute this fractal matrix

The unique-disjointness matrix ( UDISJ(n) ) is a matrix on all pairs of subsets of {1...,n} with entries $$ U_{(A,B)}=\begin{cases} 0, ~ if ~ |A\cap B|=1\\ 1, ~ otherwise \end{cases} $$

Or a bit less mathematical, it is the 2n times 2n matrix with a 0 in all entries where both indices have exactly one common 1 bit in their binary representation and a 1 in all other entries.

Example:

 0 1 10 11 100 101 110 111
 _______________________________
0 | 1 1 1 1 1 1 1 1
1 | 1 0 1 0 1 0 1 0
10 | 1 1 0 0 1 1 0 0
11 | 1 0 0 1 1 0 0 1
100 | 1 1 1 1 0 0 0 0
101 | 1 0 1 0 0 1 0 1
110 | 1 1 0 0 0 0 1 1
111 | 1 0 0 1 0 1 1 1

Write a program or function that takes an integer n as input and outputs the corresponding \2ドル^n \times 2^n\$ matrix.

Rules:

  • The values in the matrix can either be 0 / 1 (as numbers) or truthy/falsey
  • You are not allowed to permute the rows/columns of the matrix
  • This is the shortest solution (per language) wins
  • Your solution only needs to handle n between 2 and 10 (inclusive)

First 4 elements:

n=1:
1 1
1 
n=2:
1 1 1 1
1 1 
1 1 
1 1
n=3:
1 1 1 1 1 1 1 1
1 1 1 1 
1 1 1 1 
1 1 1 1
1 1 1 1 
1 1 1 1 
1 1 1 1
1 1 1 1 1
n=4:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ 52 bytes with Ruby 2.7 Numbered Parameters (TIO is stuck on 2.5.5) - ->n{(w=0...1<<n).map{|i|w.map{a=i&_1;(a&a-1)**a>0}}} \$\endgroup\$ Commented Jul 27, 2023 at 20:10
  • \$\begingroup\$ @ValueInk oh wow thanks, that's new! You really have to check your versions. I still miss when you could write .5 instead of 0.5 \$\endgroup\$ Commented Jul 27, 2023 at 21:20
  • \$\begingroup\$ CG One Handed pointed it out to me earlier this week and it feels like a whole new world. Also, you can still save a byte on 2.5 by using j&=i;(j&j-1)**j>0 for 53 bytes Try it online! \$\endgroup\$ Commented Jul 27, 2023 at 22:40

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