在这里,我们将看到如何使用C ++生成Tribonacci数。Tribonacci数与Fibonacci数相似,但是在这里我们通过添加三个前项生成一个项。假设我们要生成T(n),则公式将如下所示-
T(n) = T(n - 1) + T(n - 2) + T(n - 3)
开始的前几个数字是{0,1,1}
tribonacci(n): Begin first := 0, second := 1, third := 1 print first, second, third for i in range n – 3, do next := first + second + third print next first := second second := third third := next done End
#include<iostream> using namespace std; long tribonacci_gen(int n){ //函数生成n个正弦编号 int first = 0, second = 1, third = 1; cout << first << " " << second << " " << third << " "; for(int i = 0; i < n - 3; i++){ int next = first + second + third; cout << next << " "; first = second; second = third; third = next; } } main(){ tribonacci_gen(15); }
输出结果
0 1 1 2 4 7 13 24 44 81 149 274 504 927 1705