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

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find nth Hermite number
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find nth Hermite number
  
// Function to return nth Hermite number
function 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
echo getHermiteNumber($n);
  
// This code is contributed by ajit.
?>

chevron_right


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,
Hermite number equation

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

Below is the implementing of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find nth Hermite number
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find nth Hermite number
  
// Utility function to calculate double
// factorial of a number
function doubleFactorial($n)
{
    $fact = 1;
  
    for ($i = 1; $i <= $n; $i = $i + 2) 
    {
        $fact = $fact * $i;
    }
  
    return $fact;
}
  
// Function to return nth Hermite number
function 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
$n = 6;
  
// Print nth Hermite number
echo hermiteNumber($n);
      
// This code is contributed by akt_mit
?>

chevron_right


Output:

-120


My Personal Notes arrow_drop_up

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.