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