在这里,我们将看到如何使用C ++生成Tetranacci数字。Tetranacci数与Fibonacci数类似,但是在这里我们通过添加四个先前的项来生成一个项。假设我们要生成T(n),则公式将如下所示-
T(n) = T(n - 1) + T(n - 2) + T(n - 3) + T(n - 4)
前几个数字为{0,1,1,2}
tetranacci(n): Begin first := 0, second := 1, third := 1, fourth := 2 print first, second, third, fourth for i in range n – 4, do next := first + second + third + fourth print next first := second second := third third := fourth fourth := next done End
#include<iostream>
using namespace std;
long tetranacci_gen(int n){
//函数生成n个正弦编号
int first = 0, second = 1, third = 1, fourth = 2;
cout << first << " " << second << " " << third << " " << fourth << " ";
for(int i = 0; i < n - 4; i++){
int next = first + second + third + fourth;
cout << next << " ";
first = second;
second = third;
third = fourth;
fourth = next;
}
}
main(){
tetranacci_gen(15);
}输出结果
0 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872