Wednesday, July 3, 2024

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




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




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




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




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


Javascript




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

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




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




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


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#




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


Javascript




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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments