Encuentra el enésimo número de Hermite

Dado un entero positivo n , la tarea es imprimir el n-ésimo número de Hermite. 
Número de Hermite : en matemáticas, los números de Hermite son valores de polinomios de Hermite en argumentos cero. 
La relación de recurrencia de los polinomios de Hermite en x = 0 viene dada por, 
 

H n = -2 * (n – 1) * H n – 2 
donde H 0 = 1 y H 1 = 0 
 

Los primeros términos de la secuencia numérica de Hermite son: 
 

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

Ejemplos: 
 

Entrada: n = 6 
Salida: -120
Entrada: n = 8 
Salida: 1680 
 

Enfoque ingenuo: escriba una función recursiva que implemente la relación de recurrencia anterior.
A continuación se muestra la implementación del enfoque anterior:
 

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>
Producción: 

-120

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(n)

Enfoque eficiente: Está claro a partir de la secuencia de Hermite que si n es impar , entonces el n-ésimo número de Hermite será 0 . Ahora, el enésimo número de Hermite se puede encontrar usando, 
 

Hermite number equation

Donde (n – 1)!! = 1 * 3 * 5 * (n – 1) es decir , factorial doble de (n – 1)
A continuación se muestra la implementación del enfoque anterior:
 

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>
Producción: 

-120

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ihritik y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *