# Find nth Hermite number

Given a positive integer n, the task is to print the nth Hermite number.
Hermite Number : In mathematics, Hermite numbers are values of Hermite Polynomials at zero arguments.

The Recurrence Relation of Hermite polynomials at x = 0 is given by,

Hn = -2 * (n – 1) * Hn – 2
where H0 = 1 and H1 = 0

First few terms of Hermite number sequence are:

1, 0, -2, 0, 12, 0, -120, 0, 1680, 0, -30240

Examples:

Input: n = 6
Output: -120

Input: n = 8
Output: 1680

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: Write a recursive function implementing the above recurrence relation.

Below is the implementation of the above approach:

 `// C++ program to find nth Hermite number ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return nth Hermite number ` `int` `getHermiteNumber(``int` `n) ` `{ ` ` `  `    ``// Base conditions ` `    ``if` `(n == 0) ` `        ``return` `1; ` `    ``if` `(n == 1) ` `        ``return` `0; ` ` `  `    ``else` `        ``return` `-2 * (n - 1) * getHermiteNumber(n - 2); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 6; ` ` `  `    ``// Print nth Hermite number ` `    ``cout << getHermiteNumber(n); ` `    ``return` `0; ` `} `

 `// Java program to find nth Hermite number ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to return nth Hermite number ` `    ``static` `int` `getHermiteNumber(``int` `n) ` `    ``{ ` ` `  `        ``// Base condition ` `        ``if` `(n == ``0``) ` `            ``return` `1``; ` ` `  `        ``else` `if` `(n == ``1``) ` `            ``return` `1``; ` ` `  `        ``else` `            ``return` `-``2` `* (n - ``1``) * getHermiteNumber(n - ``2``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``6``; ` ` `  `        ``// Print nth Hermite number ` `        ``System.out.println(getHermiteNumber(n)); ` `    ``} ` `} `

 `# Python3 program to find nth Hermite number ` ` `  `# Function to return nth Hermite number ` `def` `getHermiteNumber( n): ` ` `  `    ``# Base conditions ` `    ``if` `n ``=``=` `0` `: ` `        ``return` `1` `    ``if` `n ``=``=` `1` `: ` `        ``return` `0` ` `  `    ``else` `: ` `        ``return` `(``-``2` `*` `(n ``-` `1``) ``*` `                ``getHermiteNumber(n ``-` `2``)) ` ` `  `# Driver Code ` `n ``=` `6` ` `  `# Print nth Hermite number ` `print``(getHermiteNumber(n)); ` ` `  `# This code is contributed  ` `# by Arnab Kundu `

 `// C# program to find nth Hermite number ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to return nth Hermite number ` `    ``static` `int` `getHermiteNumber(``int` `n) ` `    ``{ ` ` `  `        ``// Base condition ` `        ``if` `(n == 0) ` `            ``return` `1; ` ` `  `        ``else` `if` `(n == 1) ` `            ``return` `1; ` ` `  `        ``else` `            ``return` `-2 * (n - 1) * getHermiteNumber(n - 2); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 6; ` ` `  `        ``// Print nth Hermite number ` `        ``Console.WriteLine(getHermiteNumber(n)); ` `    ``} ` `} `

 ` `

Output:
```-120
```

Efficient Approach: It is clear from the Hermite sequence that if n is odd then nth Hermite number will be 0. Now, nth Hermite number can be found using,

Where (n – 1)!! = 1 * 3 * 5 * (n – 1) i.e. double factorial of (n – 1)

Below is the implementing of the above approach:

 `// C++ program to find nth Hermite number ` `#include ` `using` `namespace` `std; ` ` `  `// Utility function to calculate ` `// double factorial of a number ` `int` `doubleFactorial(``int` `n) ` `{ ` ` `  `    ``int` `fact = 1; ` ` `  `    ``for` `(``int` `i = 1; i <= n; i = i + 2) { ` ` `  `        ``fact = fact * i; ` `    ``} ` ` `  `    ``return` `fact; ` `} ` ` `  `// Function to return nth Hermite number ` `int` `hermiteNumber(``int` `n) ` `{ ` ` `  `    ``// If n is even then return 0 ` `    ``if` `(n % 2 == 1) ` `        ``return` `0; ` ` `  `    ``// If n is odd ` `    ``else` `{ ` ` `  `        ``// Calculate double factorial of (n-1) ` `        ``// and multiply it with 2^(n/2) ` `        ``int` `number = (``pow``(2, n / 2)) * doubleFactorial(n - 1); ` ` `  `        ``// If n/2 is odd then ` `        ``// nth Hermite number will be negative ` `        ``if` `((n / 2) % 2 == 1) ` `            ``number = number * -1; ` ` `  `        ``// Return nth Hermite number ` `        ``return` `number; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 6; ` ` `  `    ``// Print nth Hermite number ` `    ``cout << hermiteNumber(n); ` `    ``return` `0; ` `} `

 `// Java program to find nth Hermite number ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Utility function to calculate ` `    ``// double factorial of a number ` `    ``static` `int` `doubleFactorial(``int` `n) ` `    ``{ ` ` `  `        ``int` `fact = ``1``; ` ` `  `        ``for` `(``int` `i = ``1``; i <= n; i = i + ``2``) { ` ` `  `            ``fact = fact * i; ` `        ``} ` ` `  `        ``return` `fact; ` `    ``} ` ` `  `    ``// Function to return nth Hermite number ` `    ``static` `int` `hermiteNumber(``int` `n) ` `    ``{ ` ` `  `        ``// If n is even then return 0 ` `        ``if` `(n % ``2` `== ``1``) ` `            ``return` `0``; ` ` `  `        ``// If n is odd ` `        ``else` `{ ` ` `  `            ``// Calculate double factorial of (n-1) ` `            ``// and multiply it with 2^(n/2) ` `            ``int` `number = (``int``)(Math.pow(``2``, n / ``2``)) * doubleFactorial(n - ``1``); ` ` `  `            ``// If n/2 is odd then ` `            ``// nth Hermite number will be negative ` `            ``if` `((n / ``2``) % ``2` `== ``1``) ` `                ``number = number * -``1``; ` ` `  `            ``// Return nth Hermite number ` `            ``return` `number; ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``6``; ` ` `  `        ``// Print nth Hermite number ` `        ``System.out.println(hermiteNumber(n)); ` `    ``} ` `} `

 `# Python 3 program to find nth ` `# Hermite number ` `from` `math ``import` `pow` ` `  `# Utility function to calculate ` `# double factorial of a number ` `def` `doubleFactorial(n): ` `    ``fact ``=` `1` ` `  `    ``for` `i ``in` `range``(``1``, n ``+` `1``, ``2``): ` `        ``fact ``=` `fact ``*` `i ` ` `  `    ``return` `fact ` ` `  `# Function to return nth Hermite number ` `def` `hermiteNumber(n): ` `     `  `    ``# If n is even then return 0 ` `    ``if` `(n ``%` `2` `=``=` `1``): ` `        ``return` `0` ` `  `    ``# If n is odd ` `    ``else``: ` `         `  `        ``# Calculate double factorial of (n-1) ` `        ``# and multiply it with 2^(n/2) ` `        ``number ``=` `((``pow``(``2``, n ``/` `2``)) ``*`  `                   ``doubleFactorial(n ``-` `1``)) ` ` `  `        ``# If n/2 is odd then nth Hermite  ` `        ``# number will be negative ` `        ``if` `((n ``/` `2``) ``%` `2` `=``=` `1``): ` `            ``number ``=` `number ``*` `-``1` ` `  `        ``# Return nth Hermite number ` `        ``return` `number ` `     `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `6` ` `  `    ``# Print nth Hermite number ` `    ``print``(``int``(hermiteNumber(n))) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

 `// C# program to find nth Hermite number ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Utility function to calculate ` `    ``// double factorial of a number ` `    ``static` `int` `doubleFactorial(``int` `n) ` `    ``{ ` ` `  `        ``int` `fact = 1; ` ` `  `        ``for` `(``int` `i = 1; i <= n; i = i + 2) { ` ` `  `            ``fact = fact * i; ` `        ``} ` ` `  `        ``return` `fact; ` `    ``} ` ` `  `    ``// Function to return nth Hermite number ` `    ``static` `int` `hermiteNumber(``int` `n) ` `    ``{ ` ` `  `        ``// If n is even then return 0 ` `        ``if` `(n % 2 == 1) ` `            ``return` `0; ` ` `  `        ``// If n is odd ` `        ``else` `{ ` ` `  `            ``// Calculate double factorial of (n-1) ` `            ``// and multiply it with 2^(n/2) ` `            ``int` `number = (``int``)(Math.Pow(2, n / 2)) * doubleFactorial(n - 1); ` ` `  `            ``// If n/2 is odd then ` `            ``// nth Hermite number will be negative ` `            ``if` `((n / 2) % 2 == 1) ` `                ``number = number * -1; ` ` `  `            ``// Return nth Hermite number ` `            ``return` `number; ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 6; ` ` `  `        ``// Print nth Hermite number ` `        ``Console.WriteLine(hermiteNumber(n)); ` `    ``} ` `} `

 ` `

Output:
```-120
```

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.

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :