Eulerian Number - GeeksforGeeks

07 Aug, 2021
In combinatorics, the Eulerian Number A(n, m), is the number of permutations of the numbers 1 to n in which exactly m elements are greater than previous element.
For example, there are 4 permutations of the number 1 to 3 in which exactly 1 element is greater than the previous elements. 
Examples :  
Input : n = 3, m = 1
Output : 4
Please see above diagram (There
are 4 permutations where 1 no. is
greater.

Input : n = 4, m = 1
Output : 11
Eulerian Numbers are the coefficients of the Eulerian polynomials described below. 
The Eulerian polynomials are defined by the exponential generating function 
The Eulerian polynomials can be computed by the recurrence 
 
An explicit formula for A(n, m) is 
We can calculate A(n, m) by recurrence relation: 
Example: Suppose, n = 3 and m = 1. Therefore, A(3, 1) = (3 – 1) * A(2, 0) + (1 + 1) * A(2, 1) = 2 * A(2, 0) + 2 * A(2, 1) = 2 * 1 + 2 * ( (2 – 1) * A(1, 0) + (1 + 1) * A(1, 1)) = 2 + 2 * (1 * 1 + 2 * ((1 – 1) * A(0, 0) + (1 + 1) * A(0, 1)) = 2 + 2 * (1 + 2 * (0 * 1 + 2 * 0) = 2 + 2 * (1 + 2 * 0) = 2 + 2 * 1 = 2 + 2 = 4 We can verify this with example shown above.
Below is the implementation of finding A(n, m):  
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find Eulerian number A(n, m)#include <bits/stdc++.h>using namespace std; // Return euleriannumber A(n, m)int eulerian(int n, int m){    if (m >= n || n == 0)        return 0;     if (m == 0)        return 1;     return (n - m) * eulerian(n - 1, m - 1) +           (m + 1) * eulerian(n - 1, m);} // Driven Programint main(){    int n = 3, m = 1;    cout << eulerian(n, m) << endl;    return 0;}
// Java program to find Eulerian number A(n, m)import java.util.*; class Eulerian{    // Return eulerian number A(n, m)    public static int eulerian(int n, int m)    {        if (m >= n || n == 0)            return 0;         if (m == 0)            return 1;         return (n - m) * eulerian(n - 1, m - 1) +            (m + 1) * eulerian(n - 1, m);    }         // driver code       public static void main(String[] args)    {        int n = 3, m = 1;        System.out.print( eulerian(n, m) );    }} // This code is contributed by rishabh_jain
# Python3 Program to find Eulerian number A(n, m) # Return euleriannumber A(n, m)def eulerian(n, m):    if (m >= n or n == 0):        return 0;     if (m == 0):        return 1;     return ((n - m) * eulerian(n - 1, m - 1) +            (m + 1) * eulerian(n - 1, m)) # Driver coden = 3m = 1print( eulerian(n, m) ) # This code is contributed by rishabh_jain
// C# program to find Eulerian number A(n, m)using System; class Eulerian {         // Return eulerian number A(n, m)    public static int eulerian(int n, int m)    {        if (m >= n || n == 0)            return 0;         if (m == 0)            return 1;         return (n - m) * eulerian(n - 1, m - 1) +                    (m + 1) * eulerian(n - 1, m);    }     // driver code    public static void Main()    {        int n = 3, m = 1;        Console.WriteLine(eulerian(n, m));    }} // This code is contributed by vt_m
<?php// PHP Program to find// Eulerian number A(n, m) // Return euleriannumber A(n, m)function eulerian($n, $m){    if ($m >= $n || $n == 0)        return 0;     if ($m == 0)        return 1;     return ($n - $m) * eulerian($n - 1, $m - 1) +                 ($m + 1) * eulerian($n - 1, $m);} // Driven Code$n = 3; $m = 1;echo eulerian($n, $m); // This code is contributed by anuj_67.?>
<script> // JavaScript Program to find Eulerian number A(n, m)     // Return eulerian number A(n, m)    function eulerian(n, m)    {        if (m >= n || n == 0)            return 0;           if (m == 0)            return 1;           return (n - m) * eulerian(n - 1, m - 1) +            (m + 1) * eulerian(n - 1, m);    } // Driver code                 let n = 3, m = 1;        document.write( eulerian(n, m) );                   </script>
Output : 
4
Below is the implementation of finding A(n, m) using Dynamic Programming: 
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find Eulerian number A(n, m)#include <bits/stdc++.h>using namespace std; // Return euleriannumber A(n, m)int eulerian(int n, int m){    int dp[n + 1][m + 1];     memset(dp, 0, sizeof(dp));     // For each row from 1 to n    for (int i = 1; i <= n; i++) {         // For each column from 0 to m        for (int j = 0; j <= m; j++) {             // If i is greater than j            if (i > j) {                 // If j is 0, then make that                // state as 1.                if (j == 0)                    dp[i][j] = 1;                 // basic recurrence relation.                else                    dp[i][j] = ((i - j) *                     dp[i - 1][j - 1]) +                    ((j + 1) * dp[i - 1][j]);            }        }    }     return dp[n][m];} // Driven Programint main(){    int n = 3, m = 1;    cout << eulerian(n, m) << endl;    return 0;}
// Java program to find Eulerian number A(n, m)import java.util.*; class Eulerian{    // Return euleriannumber A(n, m)    public static int eulerian(int n, int m)    {        int[][] dp = new int[n+1][m+1];         // For each row from 1 to n        for (int i = 1; i <= n; i++) {                 // For each column from 0 to m            for (int j = 0; j <= m; j++) {                 // If i is greater than j                if (i > j) {                     // If j is 0, then make                    // that state as 1.                    if (j == 0)                        dp[i][j] = 1;                     // basic recurrence relation.                    else                        dp[i][j] = ((i - j) *                            dp[i - 1][j - 1]) +                        ((j + 1) * dp[i - 1][j]);                }            }        }         return dp[n][m];    }         // driver code    public static void main(String[] args)    {        int n = 3, m = 1;        System.out.print( eulerian(n, m) );    }} // This code is contributed by rishabh_jain
# Python3 Program to find Eulerian# number A(n, m) # Return euleriannumber A(n, m)def eulerian(n, m):    dp = [[0 for x in range(m+1)]             for y in range(n+1)]     # For each row from 1 to n    for i in range(1, n+1):         # For each column from 0 to m        for j in range(0, m+1):             # If i is greater than j            if (i > j):                # If j is 0, then make that                # state as 1.                 if (j == 0):                    dp[i][j] = 1                 # basic recurrence relation.                else :                    dp[i][j] = (((i - j) *                       dp[i - 1][j - 1]) +                       ((j + 1) * dp[i - 1][j]))                         return dp[n][m] # Driven Programn = 3m = 1print(eulerian(n, m)) # This code is contributed by Prasad Kshirsagar
// C# program to find Eulerian number A(n, m)using System; class Eulerian {         // Return euleriannumber A(n, m)    public static int eulerian(int n, int m)    {        int[, ] dp = new int[n + 1, m + 1];         // For each row from 1 to n        for (int i = 1; i <= n; i++) {             // For each column from 0 to m            for (int j = 0; j <= m; j++) {                 // If i is greater than j                if (i > j) {                     // If j is 0, then make                    // that state as 1.                    if (j == 0)                        dp[i, j] = 1;                     // basic recurrence relation.                    else                        dp[i, j] = ((i - j) * dp[i - 1, j - 1]) +                                        ((j + 1) * dp[i - 1, j]);                }            }        }         return dp[n, m];    }     // driver code    public static void Main()    {        int n = 3, m = 1;        Console.WriteLine(eulerian(n, m));    }} // This code is contributed by vt_m
<?php// PHP Program to find Eulerian// number A(n, m) // Return euleriannumber A(n, m)function eulerian($n, $m){    $dp = array(array());         for ($i = 0; $i < $n + 1; $i++)        for($j = 0; $j < $m + 1; $j++)            $dp[$i][$j] = 0 ;     // For each row from 1 to n    for ($i = 1; $i <= $n; $i++)    {         // For each column from 0 to m        for ($j = 0; $j <= $m; $j++)        {             // If i is greater than j            if ($i > $j)            {                 // If j is 0, then make that                // state as 1.                if ($j == 0)                    $dp[$i][$j] = 1;                 // basic recurrence relation.                else                    $dp[$i][$j] = (($i - $j) *                                    $dp[$i - 1][$j - 1]) +                                  (($j + 1) * $dp[$i - 1][$j]);            }        }    }     return $dp[$n][$m];} // Driver Code$n = 3 ;$m = 1;echo eulerian($n, $m) ; // This code is contributed by Ryuga?>
<script> // Javascript Program to find// Eulerian number A(n, m) // Return euleriannumber A(n, m)function eulerian(n, m){    var dp = Array.from(Array(n+1),    ()=> Array(m+1).fill(0));     // For each row from 1 to n    for (var i = 1; i <= n; i++) {         // For each column from 0 to m        for (var j = 0; j <= m; j++) {             // If i is greater than j            if (i > j) {                 // If j is 0, then make that                // state as 1.                if (j == 0)                    dp[i][j] = 1;                 // basic recurrence relation.                else                    dp[i][j] = ((i - j) *                     dp[i - 1][j - 1]) +                    ((j + 1) * dp[i - 1][j]);            }        }    }     return dp[n][m];} // Driven Programvar n = 3, m = 1;document.write( eulerian(n, m) ); </script>
Output :  
4
 
vt_m
Prasad_Kshirsagar
ankthon
sanjoy_62
noob2000
simranarora5sos
number-theory
Combinatorial
Dynamic Programming
number-theory
Dynamic Programming
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinational Sum
Count ways to reach the nth stair using step 1, 2 or 3
Print all possible strings of length k that can be formed from a set of n characters
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
0-1 Knapsack Problem | DP-10
Largest Sum Contiguous Subarray
Longest Common Subsequence | DP-4
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16