Tribonacci Word คือลำดับของตัวเลข คล้ายกับ Fibonacci Words Tribonacci Word สร้างขึ้นจากการต่อท้าย (concatenation) ของสามสตริงก่อนหน้าซ้ำๆ
T(n) = T(n - 1) + T(n - 2) + T(n - 3)
สตริงแรกๆ คือ {1, 12, 1213} ดังนั้นตัวถัดไปคือ 1213 + 12 + 1 = 1213121
อัลกอริทึม
tribonacci_word(n): Begin first := 1, second := 12, third := 1213 print first, second, third for i in range 3 to n, do temp := third third := third + second + first print third first := second second := next done End
ตัวอย่าง
#include<iostream> using namespace std; long tribonacci_word_gen(int n){ //function to generate n tetranacci words string first = "1"; string second = "12"; string third = "1213"; cout << first << "\n" << second << "\n" << third << "\n"; string tmp; for (int i = 3; i <= n; i++) { tmp = third; third += (second + first); cout << third <<endl; first = second; second = tmp; } } main(){ tribonacci_word_gen(6); }ผลลัพธ์
1 12 1213 1213121 1213121121312 121312112131212131211213 12131211213121213121121312131211213121213121