Function Repository Resource:

SubsetFromIndex

Source Notebook

Get the subset with the given index and length

Contributed by: Ed Pegg Jr

ResourceFunction["SubsetFromIndex"][index,len]

returns a subset of length len with given index.

Details and Options

Subsets are considered to be lists {a1,a2,,ak} such that the ai are non-negative integers and strictly increasing.
Subsets are sorted by their reverse; {1,2,3} comes before {0,1,4}.
For any subset, a unique index n can be defined via .
For example: subset {2,4,6} has index .

Examples

Basic Examples (3) 

Give the first ten 3-subsets:

In[1]:=
Table[ResourceFunction["SubsetFromIndex"][index, 3], {index, 1, 10}]
Out[1]=

The following code gives the same 3-subset sequence:

In[2]:=
SortBy[Subsets[Range[0, 4], {3}], Reverse]
Out[2]=

The function can return subsets of a large index:

In[3]:=
Table[ResourceFunction["SubsetFromIndex"][index, 3], {index, 182000, 182400, 47}]
Out[3]=

The function can return the subset of a given index for various lengths:

In[4]:=
Table[ResourceFunction["SubsetFromIndex"][2019, len], {len, 2, 9}]
Out[4]=

Scope (3) 

Some binomial representations of the number 320:

In[5]:=
Column[Row[
    Riffle[Table[
      TraditionalForm[
       Binomial[(ToString /@ #)[[n]], ToString[n]]], {n, 1, Length[#]}], "+"]] & /@ Table[ResourceFunction["SubsetFromIndex"][321, n], {n, 2, 7}], Alignment -> Center]
Out[5]=

Here are some 2-subsets with their indices to show their structure:

In[6]:=
doubles = Table[{ResourceFunction["SubsetFromIndex"][index, 2], index}, {index, 1, 15}];
Graphics[{Text[Style[Column[{StringJoin[ToString /@ #[[1]]], #[[2]]},
       Alignment -> Center], 20], #[[1]]] & /@ doubles, Blue, Line[First /@ doubles]}]
Out[6]=

The structure of 3-subsets in 3D:

In[7]:=
triples = Table[{ResourceFunction["SubsetFromIndex"][index, 3], index}, {index, 1, 20}];
Graphics3D[{Text[Style[Column[{StringJoin[ToString /@ #[[1]]], #[[2]]},
       Alignment -> Center], 20], #[[1]]] & /@ triples, Blue, Line[First /@ triples]}]
Out[7]=

Neat Examples (1) 

Find the trillionth numbers with binary weights eight to twelve:

In[8]:=
Column[Table[
  Total[2^ResourceFunction["SubsetFromIndex"][10^12, weight]], {weight, 8, 12}]]
Out[8]=

Version History

  • 2.0.0 – 24 December 2019
  • 1.0.0 – 05 December 2019

Related Resources

License Information