Function Repository Resource:

TupleFromIndex

Source Notebook

Return the tuple corresponding to a given index

Contributed by: Ed Pegg Jr

ResourceFunction["TupleFromIndex"][index,k]

returns index from the list of integer k-tuples sorted by maximal element.

Details and Options

A tuple with largest element n will be ordered after tuples that have all elements less than n.
In case of ties, tuples with largest element n have Sort ordering.

Examples

Basic Examples (5) 

Create 2-tuples from their index:

In[1]:=
Table[ResourceFunction["TupleFromIndex"][n, 2], {n, 1, 9}]
Out[1]=

The first nine 2-tuples sorted by maximal element:

In[2]:=
SortBy[Tuples[{0, 1, 2}, {2}], Max[#] &]
Out[2]=

Create 3-tuples from their index:

In[3]:=
Table[ResourceFunction["TupleFromIndex"][n, 3], {n, 1, 27}]
Out[3]=

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

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

Find the trillionth k-tuple for several values of k:

In[5]:=
Table[ResourceFunction["TupleFromIndex"][10^12, k], {k, 2, 12}]
Out[5]=

Properties and Relations (2) 

Use Tuples to produce 3-tuples sorted by maximal element:

In[6]:=
orderbymax = SortBy[Tuples[Range[0, 2], {3}], {Max[#], #} &]
Out[6]=

TupleFromIndex returns the same tuples in the same sequence:

In[7]:=
Table[ResourceFunction["TupleFromIndex"][index, 3], {index, 1, 27}]
Out[7]=

Neat Examples (2) 

The googolth 6-tuple:

In[8]:=
ResourceFunction["TupleFromIndex"][10^100, 6]
Out[8]=

The googolth 111-tuple:

In[9]:=
ResourceFunction["TupleFromIndex"][10^100, 111]
Out[9]=

Version History

  • 2.0.0 – 20 December 2019
  • 1.0.0 – 02 December 2019

Related Resources

License Information