यहां हम देखेंगे कि C++ का उपयोग करके टेट्रानैकी नंबर्स कैसे जेनरेट करें। टेट्रानैकी नंबर्स फिबोनैकी नंबर्स के समान हैं, लेकिन यहां हम चार पिछले टर्म्स को जोड़कर एक टर्म जेनरेट कर रहे हैं। मान लीजिए हम 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 i को n – 4 की रेंज में, करें 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