在C++中,Motzkin数

在C++中,Motzkin数

The Motzkin number series starts with 1, 1, 4, 9, etc.., We can get the generalised nth term with the sequence. The Motzkin number sequence is as follows.

a0 = 1

a1 = 1

a2 = 4

a3 = 9

an = ((2 * n + 1)/ n + 2) * M(n-1) +((3 * n – 3)/ n + 2) * M(n – 2)

Algorithm

  • Initialise the number n.

  • Iterate till n.

    • Update the previous two numbers

  • Return the last number.

Example

Implementation

Following is the implementation of the above algorithm in C++

#include
using namespace std;
int getNthTerm(int n) {
if(n == 0 || n == 1) {
return 1;
}
int a = 1, b = 1;
for(int i = 2; i

上一篇 PHP网站性能优化:如何优化文件上传流程以提高访问速度?
下一篇 C++在嵌入式系统开发中的外围设备控制与数据传输功能实践
Escape

Escape

这个人很懒,什么都没有留下~

本月创作热力图