
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find nth Hermite number in C++
In this problem, we are given an integer value N. Our task is to create a program to Find nth Hermite number.
Hermite Number is a number is the value of hermite number when there are 0 arguments.
Nth hermite Number is HN = (-2) * (N - 1) * H(N-2) The base values are H0 = 1 and H0 = 0.
The hermite sequence is − 1, 0, -2, 0, 12, 0, -120, 0, 1680, 0….
Let’s take an example to understand the problem,
Input
N = 7
Output
0
Input
N = 6
Output
-120
Solution Approach
A simple solution to the problem is using the formula for hermite number. This is done using recursion, we can find Nth Term.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int calcNHermiteNumber(int N) { if (N == 0) return 1; if (N % 2 == 1) return 0; else return -2 * (N - 1) * calcNHermiteNumber(N - 2); } int main() { int N = 10; cout<<"The "<<N<<"th hermite Number is "<<calcNHermiteNumber(N); return 0; }
Output
The 10th hermite Number is -30240
Efficient approach
An efficient approach to solve the problem is by using the formula. We can derive the general formula using the recursive formula.
Here, if the value of N is odd, the hermite number is 0.
If the value of N is even, their will be some value defined by the formula,
HN = ( (-1)(N/2)) * ( 2(N/2) ) * (N-1)!!
The (N-1)!! Is semi-factorial which is calculated as (n-1)*(n-3)*...3*1.
Program to illustrate the working of our solution,
Example
#include <iostream> #include <math.h> using namespace std; int calcSemiFact(int n) { int factVal = 1; for (int i = 1; i <= n; i = i + 2) { factVal *= i; } return factVal; } int calcNHermiteNumber(int n) { if (n % 2 == 1) return 0; int HermiteNumber = (pow(2, n / 2)) * calcSemiFact(n - 1); if ((n / 2) % 2 == 1) HermiteNumber *= -1; return HermiteNumber; } int main() { int N = 10; cout<<"The "<<N<<"th hermite Number is "<<calcNHermiteNumber(N); return 0; }
Output
The 10th hermite Number is -30240
- Related Articles
- Find the Nth Ugly Number in Java
- Program to find Nth Fibonacci Number in Python
- Program to find nth ugly number in C++
- Program to find Nth Even Fibonacci Number in C++
- C++ program to find Nth Non Fibonacci Number
- Nth Magical Number in C++
- 8085 program to find nth power of a number
- Find Nth positive number whose digital root is X in C++
- Nth Catalan Number in Python Program
- Nth Catalan Number in Go Lang
- Program to find nth smallest number from a given matrix in Python
- Program to find last two digits of Nth Fibonacci number in C++
- Finding the nth prime number in JavaScript
- Find the Nth Number Made Up of only Odd Digits using C++
- Find nth number that contains the digit k or divisible by k in C++
