Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Eulerian Number

  • Difficulty Level : Medium
  • Last Updated : 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. 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Eulerian



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++




// 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 Program
int main()
{
    int n = 3, m = 1;
    cout << eulerian(n, m) << endl;
    return 0;
}

Java




// 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




# 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 code
n = 3
m = 1
print( eulerian(n, m) )
 
# This code is contributed by rishabh_jain

C#




// 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
// 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.
?>

Javascript




<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++




// 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 Program
int main()
{
    int n = 3, m = 1;
    cout << eulerian(n, m) << endl;
    return 0;
}

Java




// 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




# 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 Program
n = 3
m = 1
print(eulerian(n, m))
 
# This code is contributed by Prasad Kshirsagar

C#




// 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
// 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
?>

Javascript




<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 Program
var n = 3, m = 1;
document.write( eulerian(n, m) );
 
</script>

Output :  

4

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!