A351939 Irregular triangle read by rows: the n-th row contains the values 0..2^n-1 sorted first by Hamming weight and then by position in reflected Gray code.
0, 0, 1, 0, 1, 2, 3, 0, 1, 2, 4, 3, 6, 5, 7, 0, 1, 2, 4, 8, 3, 6, 5, 12, 10, 9, 7, 13, 14, 11, 15, 0, 1, 2, 4, 8, 16, 3, 6, 5, 12, 10, 9, 24, 20, 18, 17, 7, 13, 14, 11, 25, 26, 28, 21, 22, 19, 15, 27, 30, 29, 23, 31, 0, 1, 2, 4, 8, 16, 32, 3, 6, 5, 12, 10, 9, 24, 20, 18, 17, 48, 40, 36, 34, 33, 7, 13, 14, 11, 25
Offset: 0
Examples
Triangle T(n,k) begins: n=0: 0; n=1: 0, 1; n=2: 0, 1, 2, 3; n=3: 0, 1, 2, 4, 3, 6, 5, 7; n=4: 0, 1, 2, 4, 8, 3, 6, 5, 12, 10, 9, 7, 13, 14, 11, 15; n=5: 0, 1, 2, 4, 8, 16, 3, 6, 5, 12, 10, 9, 24, 20, 18, 17, 7, 13, 14, 11, 25, 26, 28, 21, 22, 19, 15, 27, 30, 29, 23, 31; ... For row n = 3, the binary words of length 3 in reflected Gray code order are 000, 001, 011, 010, 110, 111, 101, 100. Arranging these by Hamming weight but otherwise preserving the order gives 000, 001, 010, 100, 011, 110, 101, 111. As decimal numbers these are 0, 1, 2, 4, 3, 6, 5, 7, which is row 3.
References
- D. Knuth, The art of computer programming, Volume 4A: Combinatorial Algorithms, Part 1, Addison-Wesley, 2011.
- F. Ruskey, Combinatorial Generation. Working Version (1j-CSC 425/520), 2003.
Links
- Valentin Bakoev, Table of n, a(n) for n = 0..65535
- Valentin Bakoev, Some problems and algorithms related to the weight order relation on the n-dimensional Boolean cube, Discrete Mathematics, Algorithms and Applications, Vol. 13 No 3, 2150021 (2021); arXiv preprint, arXiv:1811.04421 [cs.DM], 2018-2020.
- Valentin Bakoev, Ordering the Boolean Cube Vectors by Their Weights and with Minimal Change, Int'l Conf. Algebraic Informatics (CAI 2022) Lecture Notes Comp. Sci. (LNCS) Vol. 13706, 43-54.
- Sage Reference Manual, Gray codes
Crossrefs
Programs
-
Maple
b:= proc(n) b(n):= `if`(n<2, n, Bits[Xor](n, b(iquo(n, 2)))) end: h:= proc(n) h(n):= add(i, i=Bits[Split](n)) end: T:= n-> sort([$0..2^n-1], (x,y)-> h(x)
Alois P. Heinz, Mar 01 2022 -
Mathematica
b[n_] := If[n < 2, n, BitXor[n, b[Quotient[n, 2]]]]; h[n_] := DigitCount[n, 2, 1]; T[n_] := SortBy[{h, b}][Range[0, 2^n - 1]]; Table[T[n], {n, 0, 6}] // Flatten (* Jean-François Alcover, Oct 21 2022, after Alois P. Heinz *)
-
PARI
row(n)=vecsort(vector(2^n, i, i--; bitxor(i, i>>1)), (x,y) -> cmp(hammingweight(x), hammingweight(y))) { for(n=0, 5, print(row(n))) } \\ Andrew Howroyd, Feb 28 2022
Formula
The n-th row is the concatenation of the subsequences g(n, 0), ..., g(n, n), where the subsequences are defined as follows:
g(n, 0) = (0),
g(n, n) = (2^n - 1),
g(n, k) = g(n-1, k) concatenate (g^r(n-1, k-1) + 2^(n-1)) for 0 < k < n.
In the above, g^r(n-1, k-1) + 2^(n-1) means the 2^(n-1) is added to each member of the subsequence g(n-1, k-1) in reversed order.
Comments