여기서는 C++를 사용하여 트리보나치 수를 생성하는 방법을 살펴보겠습니다. 트리보나치 수는 피보나치 수와 유사하지만 여기서는 이전 항 3개를 추가하여 항을 생성합니다. 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){
//function to generate n tetranacci numbers
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