Note
The Eulerian number is meant below. Distinguish it from the Euler number and Euler's number (mathematical constants associated with Euler, such as or ).
In combinatorics, Eulerian numbersare the number of permutations of through with exactly elements greater than their predecessors (that is, with ascents). They are defined as follows:
For example, among the numbers through , there are permutations in which exactly one element is greater than its predecessor:
| Permutation | Adjacent elements satisfying the condition | Count |
|---|---|---|
| 1 2 3 | 1, 2 & 2, 3 | 2 |
| 1 3 2 | 1, 3 | 1 |
| 2 1 3 | 1, 3 | 1 |
| 2 3 1 | 2, 3 | 1 |
| 3 1 2 | 1, 2 | 1 |
| 3 2 1 | 0 |
Thus, by the definition of , when equals and equals , the Eulerian number is , meaning that there are permutations with element greater than its predecessor.
For small values of and , Eulerian numbers can be obtained directly:
| Permutations satisfying the condition | Count | |
|---|---|---|
| 1 | ||
| 1 | ||
| 1 | ||
| 1 | ||
| 4 | ||
| 1 |
Formula
Eulerian numbers can be computed recursively or by dynamic programming.
First, when or , no permutation satisfies the condition, so the Eulerian number is .
Second, when , only the descending permutation satisfies the condition, so the Eulerian number is .
Finally, starting from a permutation of , insert to obtain a permutation of . Since inserting can increase the Eulerian number by at most , can transition only from and .
Consider inserting : when , if is inserted before , that is, is inserted into an ascent, the Eulerian number is unchanged; inserting at the beginning also leaves it unchanged; otherwise, if is inserted elsewhere, the Eulerian number increases by .
For a transition from to , the Eulerian number must increase by . Thus, cannot be inserted inside an ascent or at the beginning, leaving choices.
For a transition from to , the Eulerian number must remain unchanged. Thus, can only be inserted inside an ascent or at the beginning, giving choices.
Combining the cases gives
Implementation
C++
int eulerianNumber(int n, int m) {
if (m >= n || n == 0) return 0;
if (m == 0) return 1;
return (((n - m) * eulerianNumber(n - 1, m - 1)) +
((m + 1) * eulerianNumber(n - 1, m)));
}
Python
def eulerianNumber(n, m):
if m >= n or n == 0:
return 0
if m == 0:
return 1
return ((n - m) * eulerianNumber(n - 1, m - 1)) + (
(m + 1) * eulerianNumber(n - 1, m)
)