In: Computer Science
Design in pseudo code a linear recursive version of Tetranacci calculators.
Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
Below is the code in C++ to print tetranacci numbers:
#include <iostream>
using namespace std;
int main()
{
int i, n;
cout<<"Enter n: "; //Enter the number of tetranacci
numbers you want
cin>>n;
int a[n+4];
a[0] = a[1] = a[2] = 0; // Define the first four elements as we
need them to calculate sum
a[3] = 1;
cout<<a[0]<<" "<<a[1]<<"
"<<a[2]<<" "<<a[3]; // Print first four
elements
for(i = 4; i <= n; i++) // Run loop from 4th element to nth
element
{
a[i] = a[i-1] + a[i-2] + a[i-3] + a[i-4]; // value of each
element is equal to sum of its previous four elements
cout<<" "<<a[i];
}
return 0;
}