📜  查找第n个Hermite编号

📅  最后修改于: 2021-04-24 19:45:06             🧑  作者: Mango

给定正整数n ,任务是打印第n个Hermite号。
埃尔米特数:在数学中,埃尔米特数是零参数下的埃尔米特多项式的值。

x = 0时Hermite多项式的递归关系由下式给出:

Hermite数字序列的前几项是:

例子:

天真的方法:编写实现上述递归关系的递归函数。

下面是上述方法的实现:

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


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


输出:
-120

有效方法:从Hermite序列中可以清楚地看到,如果n奇数,第n个Hermite数将为0 。现在,可以使用以下方法找到第n个Hermite编号,
厄米数方程

哪里(n – 1)! = 1 * 3 * 5 *(N – 1)即双阶乘(N – 1)

下面是上述方法的实现:

C++

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

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


输出:
-120