Open In App

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




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




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




<script>
 
    // Javascript program to
    // find nth Hermite number
     
    // Function to return nth Hermite number
    function getHermiteNumber(n)
    {
   
        // Base condition
        if (n == 0)
            return 1;
   
        else if (n == 1)
            return 1;
   
        else
            return -2 * (n - 1) * getHermiteNumber(n - 2);
    }
     
    let n = 6;
   
    // Print nth Hermite number
    document.write(getHermiteNumber(n));
     
</script>

Output: 
-120

 

Time Complexity: O(n)

Auxiliary Space: O(n)

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




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




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




<script>
 
// Javascript program to find nth Hermite number
 
// Utility function to calculate
// double factorial of a number
function doubleFactorial(n)
{
 
    var fact = 1;
 
    for (var 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)
        var number = (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
var n = 6;
 
// Print nth Hermite number
document.write( hermiteNumber(n));
 
</script>

Output: 
-120

 

Time Complexity: O(n)

Auxiliary Space: O(1)


Article Tags :