Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

Combinatorics

Guides

  • Combinatorics
  • Functions I understand in combinatorics

Tech Notes

  • Combinatorics

Symbols

  • CanonicalMultiset
  • CentralBinomialCoefficient
  • ConjugatePartition
  • DescendingSublists
  • DivisorHasseDiagram
  • DominatingIntegerPartitionQ
  • DurfeeSquare
  • EnumerateMultisetPartialDerangements
  • EulerianCatalanNumber
  • EulerianNumber
  • EulerianNumberOfTheSecondKind
  • FerrersDiagram
  • Fibbinary
  • FibonacciEncode
  • FrobeniusSymbolFromPartition
  • FromInversionVector
  • GaussFactorial
  • GrayCode
  • HasseDiagram
  • HookLengths
  • HuffmanCodeWords
  • HuffmanDecode
  • HuffmanEncode
  • IntegerPartitionQ
  • InverseFibonacci
  • InverseGrayCode
  • InversionCount
  • InversionVectorQ
  • LehmerCodeFromPermutation
  • LucasNumberU1
  • LucasNumberV2
  • Multichoose
  • MultisetAssociation
  • MultisetPartialDerangements
  • MultisetStrictAscents
  • NarayanaNumber
  • NextPermutation
  • NumberOfTableaux
  • OrderedTupleFromIndex
  • OrderedTupleIndex
  • OrderlessCombinations
  • OrderlessCombinationsOfUnmarkedElements
  • PartialOrderGraphQ
  • PartitionCrank
  • PartitionFromFrobeniusSymbol
  • PartitionRank
  • PermutationAscents
  • PermutationCountByInversions
  • PermutationFromIndex
  • PermutationGraph
  • PermutationIndex
  • PermutationMajorIndex
  • PermutationToTableaux
  • Phitorial
  • PosetQ
  • PosetToTableau
  • Primorial
  • QExponential
  • QMultinomial
  • RandomYoungTableau
  • RationalNumberRepeatingDecimalPeriod
  • ReflexiveGraphQ
  • SecantNumber
  • SelectPermutations
  • SelectSubsets
  • SelectTuples
  • SelfConjugatePartitionQ
  • SignedLahNumber
  • StandardYoungTableaux
  • StirlingPermutations
  • SubsetFromIndex
  • SubsetIndex
  • TableauQ
  • TableauToPoset
  • TableauxToPermutation
  • TangentNumber
  • ToInversionVector
  • TransitiveGraphQ
  • TransposeTableau
  • TupleFromIndex
  • TupleIndex
  • UnsignedLahNumber
  • ZeckendorfRepresentation
Combinatorics
Indexes
Ferrers diagrams
Central Binomial Coefficient
Selecting permutations
Modified Central Binomial Coefficient
Selecting subsets
Derangements
Selecting tuples
Multichoose and orderless combinations
Eulerian Numbers
This paclet is for the study of combinatorics. I am a combinatorialist. That means I study combinatorics.
The functions defined in the
PeterBurbery`Combinatorics`
context provide support for finding solutions to combinatorics-related problems.
In[23]:=
Needs["PeterBurbery`Combinatorics`"]
Indexes
PermutationIndex
[perm]
gives the lexicographic index of permutation
perm
.
PermutationFromIndex
[index,lengthInput]
returns the permutation of length
lengthInput
corresponding to the
index
th permutation in lexicographic order.
SubsetIndex
[list]
gives the index of subset
list
.
SubsetFromIndex
[index,len]
returns a subset of length
len
with given
index
.
Computing the index or using the index to get the thing.
Find the index of a random permutation
In[2]:=
PermutationIndex
[Echo[PermutationList@RandomPermutation[9]]]
»
{4,7,6,8,1,9,5,2,3}
Out[2]=
149543
Get the permutation:
In[3]:=
PermutationFromIndex
[%,9]
Out[3]=
{4,7,6,8,1,9,5,2,3}
Here's a neat application of this function. We can use this to solve Project Euler Problem 24 Lexicographic Permutations. What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9?
In[6]:=
PermutationFromIndex
[1000000(*amillionis
6
10
*),10(*becauseLength[{0,1,2,3,4,5,6,7,8,,9}]is10,not9*)]
Out[6]=
{3,8,9,4,10,2,6,5,7,1}
Now we need to subtract 1. 10 will become 9 and 1 will become 0.
In[7]:=
PermutationFromIndex
[1000000(*amillionis
6
10
*),10(*becauseLength[{0,1,2,3,4,5,6,7,8,,9}]is10,not9*)]-1
Out[7]=
{2,7,8,3,9,1,5,4,6,0}
Now we can use FromDigits.
In[8]:=
FromDigits
PermutationFromIndex
[1000000(*amillionis
6
10
*),10(*becauseLength[{0,1,2,3,4,5,6,7,8,,9}]is10,not9*)]-1
Out[8]=
2783915460
And that's the answer!
Find a subset's index.
In[10]:=
SubsetIndex
[{2,4,6}]
Out[10]=
29
Find the subset.
In[15]:=
SubsetFromIndex
[29,3]
Out[15]=
{2,4,6}
Central Binomial Coefficient
Compute the central binomial coefficient.
In[18]:=
CentralBinomialCoefficient
[54]
Out[18]=
24857784491537440929618523018320
In[19]:=
CentralBinomialCoefficient
[n]
Out[19]=
Binomial[2n,n]
Find the generating function.
In[20]:=
GeneratingFunction
CentralBinomialCoefficient
[n],n,x
Out[20]=
1
1-4x
Make a table of values.
In[22]:=
DatasetAssociationMap"n"
CentralBinomialCoefficient
[#]&[Range[21]]
Out[22]=
n
1
2
2
6
3
20
4
70
5
252
6
924
7
3432
8
12870
9
48620
10
184756
11
705432
12
2704156
13
10400600
14
40116600
15
155117520
16
601080390
17
2333606220
18
9075135300
19
35345263800
20
137846528820
rows 1–20 of
21
The digits of
2
n
10
n
10
for
n=0,1,…
are
In[32]:=
DatasetAssociationMap"n"IntegerLength
CentralBinomialCoefficient
[
#
10
]&[Range[0,8]]
Out[32]=
n
0
1
1
6
2
59
3
601
4
6019
5
60204
6
602057
7
6020597
8
60205995
These digits are converging to the digits of
log
10
4
.
In[34]:=
N[Log[10,4],40]
Out[34]=
0.6020599913279623904274777894489860535364
Modified Central Binomial Coefficient
Let's look at the modified central binomial coefficient.
In[958]:=
Dataset[AssociationMap["n"ModifiedCentralBinomialCoefficient[#]&][Range[0,21]]]
Out[958]=
n
0
1
1
1
2
2
3
3
4
6
5
10
6
20
7
35
8
70
9
126
10
252
11
462
12
924
13
1716
14
3432
15
6435
16
12870
17
24310
18
48620
19
92378
rows 1–20 of
22
This matches our output.
If we start at 1, we will get all True instead of a False at 0.
For some reason, I can't use GeneratingFunction.
Derangements

Sets

Derangements of a set

Let's analyze the derangements. A derangement is where no element remains in its original position. If an element remains in its original position, you call that a fixed point. So a derangement is a permutation with 0 fixed points.
We can count this.
We can also compute this.
This is the subfactorial of the length.

Partial derangements of a set

We can also compute partial derangements. Let's say we have 2 fixed points. That means 2 elements do not move.
Now let's count them.

Multisets

Here is a multiset with repeated elements.
Computing derangements of multisets is harder, but the function can do it.

Derangements

Let's do a smaller example. This time, let's list all the elements of the set.
Let's find all the multiset partial derangements.
The length:
Here is the count.
Count the derangements of the whole set.
This is the same as having 0 fixed points.

Partial Derangements

List all the partial derangements with 3 fixed points one by one.
The length:
The computed length:
Count partial derangements of the whole set with 3 fixed points.
If we compute partials derangements with 3 fixed points, we will get 24 elements.
I will limit the output to 20 this time.
Multichoose and orderless combinations
There are ten candies: licorice, peppermint drops, lemon drops, truffles, gummy bears, gummy worms, chocolate bars, jelly beans, peanut butter cups, and marshmallows. There are four children named Peter, James, John, and Andrew. How many ways can the ten candies be distributed among the four children? We can compute 10 multichoose 4.
Another way of entering it is like this. This is infix notation.
We can also generate all the combinations and count them. Since there are a lot of combinations, I am not listing them all here and then using Length. I'm just going to use Length immediately.
If we want to count orderless combinations, we can use multichoose.
Get length four sets from a list of length two:
Elements need not be integers:
We could consider {"cat", "cat", "cat"} and {"dog", "dog", "dog"} as partly equal.
If we delete duplicates by CanonicalMultiset, we get fewer elements.
Here's what we would get something like.
These produce the same output.
Let's compare the performance for a large set.
The definition of the function is significantly faster and uses significantly less memory for larger inputs.
Ferrers diagrams
Make a Ferrers diagram for every partition of 5.
Rows 1 to 4 have 5, 4, 2, 2 dots:
Here is a random choice of one of the 204226 partitions of 50:
Selecting permutations
Select from the list {5,6,7,8,9} those permutations that form a prime when concatenating the digits:
Here are the numbers.
Select permutations of length 3:
Here are the numbers.
Select permutations with length 3—4:
Here are the numbers.
Select permutations for which the first two elements and the last elements add up to the same value:
Select the first ten permutations of length 4 for which the elements add up to an odd number:
Expand the polynomials.
There are really only three unique ones.
Make a graph of all of them.
Duplicated items are treated the same:
​
Verify that the results are identical:
​
Using the built-in functions is faster at the expense of 500× more memory:
Selecting subsets
Select subsets from {1,2,3,4,5} that add up to 10:
​
Select subsets of length 2 to 4 that sum up to a prime:
Select all subsets of length 2 that add up to 6:
​
Select all subsets that add up to 0:
​
Select all subsets of odd length that add up to a prime:
​
Select the first 8 subsets that add up to a prime:
Find subsets that add up to 25:
Compared to naive implementation, which requires roughly a 1000 times more memory:
Verify the result is the same:
​
Find subsets that add up to 0:
Visualize the lengths of the lists:
Selecting tuples
Find out for which 2-tuple the sum is a prime:
Only get the first five:
Find the first 15 three-letter palindromic lists:
Find vectors for which the norm is an integer:
Compared to naive implementation:
Eulerian Numbers

Eulerian Numbers

The table of Eulerian numbers up to 10:
The combinatorial interpretation of Eulerian numbers is they give the number of permutations of the numbers 1 to n in which exactly k elements are greater than the previous element (permutations with k "ascents").
Let's manually verify some of the triangle.
We have to change the number by 1.
Let's compare the two again.
The function is using a different convention.
The first 9 rows of the triangle of Eulerian numbers of the second kind:
A list:
The function works on lists.
Make a list of digits.
The Stirling permutations are related to the Eulerian number of the second kind. Here we can compute the length of the Stirling permutations.
We can compare this to the row totals in the Eulerian number of the second kind triangle.
Here is another way.
To get the list I can use Normal.
All of these are equal.

Eulerian Catalan Numbers

I got kind of confused with this.
This function uses a different index for k.
This also affects the definition of other related functions like EulerianCatalanNumber.
Let's compute the list with the definition.
Let's verify this for small cases by counting.
Here's how we can count the number of ascents of a permutation.
Now let's generate the permutations. There are too many permutations to display for n=3.
Now, let's select the permutations with n ascents.
Now let's count them.
Now let's multiply this number by 2.
This is what we would get with EulerianCatalanNumber
Let's verify this for a larger set of numbers.
I could also use SelectPermutations.
Let's see how much memory our calculation took.
Now let's use SelectPermutations. Here's an example of how we could use it.
Let's do it with Table.
How does this compare to the normal method?
We used less memory.
The first 14 rows of the Narayana triangle read as follows:
Make a table of signed and unsigned Lah numbers.
Find the sum of even-valued Fibonacci numbers less than 4 million.
Compute the inverse Fibonacci of 4 million.
That's the answer.
Here are more examples.
For some reason, although I have a function that can compute the next permutation, I have not solved this Wolfram Challenge.
Consider the permutation:
The descents follow easily:
There is also a function for this.
A triangular table of Gauss factorials up to 7::
Make a grid with a frame:
Phitorial of 10:
Compute the answer mod 1000000007:
Product of primes up to 20:
Product of primes up to 54:
Compute a list of the first 15 primorials:
Compare with the definition:
Compare the growth rate of the primorial versus factorial:
Now let's look at Lucas numbers.
You can also do the Pell numbers with LinearRecurrence:
Find a linear recurrence:
Generate a linear recurrence:
We can also find a Fibonacci function:
​
We can find a pattern:
Generate a new array:
​
​
​
Verify the statement up to 7:
​
Verify this statement:
​
The first 21 Lucas numbers:
Compare the function to LucasL:
​
​
​
Let's compare this to LucasL:
​
Test this statement:
​
Test this statement:
Evaluate the q-exponential.
Evaluate numerically:
Change q from 1 to 2.
Ask for 40 digits of precision.
Evaluate the q-multinomial.
Evaluate numerically:
Find the period of the decimal representation of a rational number.
3/2 has a period of 0.
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given.
We can use Identity@@ to get just the number.
We can use EchoPerformance to see how much memory we used.
​
Make a list for the first 1000 numbers.

© 2025 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com