Question

In: Computer Science

Problem 3.2 A more efficient version of the function for computing Tetranacci numbers can be defined...

Problem 3.2

A more efficient version of the function for computing Tetranacci numbers can be defined by following three steps:

  1. Implement a function which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g., in F#, 1::1::1::1::[] should become 4::1::1::1::1::[]).
  2. Implement a recursive function which computes the nth Tetranacci number in linear time. This function may use a linear amount of space to compute its result, but the number of recursive calls it makes must be linear in n.

Your task is to follow the three steps above, in order to implement the recursive function tetrn2 in F#.

  • Hint: Try to define auxiliary functions first before taking on main function. You do not have to use auxiliary functions in your tetrn2, but they provide an insight on how to implement the tetrn2 function.

In [ ]:

// Problem 3.2.1
let append4Sum xs =
    // write your solution here
// Problem 3.2.2
let tetrn2 n =
    // write your solution here

Test your function:

In [ ]:

 
tetrn2 33 = 747044834 // this is fast compare to `tetrn 33` call

Solutions

Expert Solution

Tetranacci Numbers

The tetranacci numbers are a generalization of the Fibonacci numbers defined by the recurrence relation

T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4)

with T(0)=0, T(1)=1, T(2)=1, T(3)=2,

For n>=4. They represent the n=4 case of the Fibonacci n-step numbers. The first few terms for n=0, 1, … are 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, …

Given a number N. The task is to find the N-th tetranacci number.

Examples:

Input: 5
Output: 4

Input: 9
Output: 108

A better solution is to use Dynamic Programming (memoisation) as there are multiple overlaps.

Given below is the recursive tree for N=10.

                                        rec(10)

                             /         /       \          \

                       rec(9)      rec(8)     rec(7)     rec(6)

              /      /     \     \
                     
          rec(8) rec(7)  rec(6)  rec(5)

In the above partial recursion tree, rec(8), rec(7), rec(6) has been solved twice. On drawing the complete recursion tree, it has been observed that there are many subproblems which are solved again and again. So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation.

Below is the implementation of the above approach in C++

// A DP based CPP

// program to print

// the nth tetranacci number

#include <iostream>

using namespace std;

// Function to print the

// N-th tetranacci number

int printTetra(int n)

{

    int dp[n + 5];

    // base cases

    dp[0] = 0;

    dp[1] = dp[2] = 1;

    dp[3] = 2;

    for (int i = 4; i <= n; i++)

        dp[i] = dp[i - 1] + dp[i - 2] +

                dp[i - 3] + dp[i - 4];

    cout << dp[n];

}

// Driver code

int main()

{

    int n = 10;

    printTetra(n);

    return 0;

}

Output:

208

Time Complexity: O(N)
Auxiliary Space: O(N)

Next Solution

The time complexity of above is linear, but it requires extra space. Space used can be optimized in the above solution by using four variables to keep track of the previous four numbers.

Below is the implementation of the above approach

// A space optimized

// based CPP program to

// print the nth tetranacci number

#include <iostream>

using namespace std;

// Function to print the

// N-th tetranacci number

void printTetra(int n)

{

    if (n < 0)

        return;

    // Initialize first

    // four numbers to base cases

    int first = 0, second = 1;

    int third = 1, fourth = 2;

    // declare a current variable

    int curr;

    if (n == 0)

        cout << first;

    else if (n == 1 || n == 2)

        cout << second;

    else if (n == 3)

        cout << fourth;

    else {

        // Loop to add previous

        // four numbers for

        // each number starting

        // from 4 and then assign

        // first, second, third

        // to second, third, fourth and

        // curr to fourth respectively

        for (int i = 4; i <= n; i++) {

            curr = first + second + third + fourth;

            first = second;

            second = third;

            third = fourth;

            fourth = curr;

        }

        cout << curr;

    }

}

// Driver code

int main()

{

    int n = 10;

    printTetra(n);

    return 0;

}

Output:

208

Time Complexity: O(N)
Auxiliary Space: O(1)


Related Solutions

The TP Company is looking to replace an existing machine with a new, more efficient version....
The TP Company is looking to replace an existing machine with a new, more efficient version. TP feels the new will extend the production life by one year while increasing cash flows. The new machine will cost $150,000 today and have a 6 year production life.It will be depreciated as 5-year MARCS property with an estimated salvage value of $20,000 at the end of its production life. The project will also set aside $6,000 for working capital. The old machine...
Consider the user-defined MATLAB function below, ht_mp_ch(). The function ht_mp_ch(), outputs the sampled version of the...
Consider the user-defined MATLAB function below, ht_mp_ch(). The function ht_mp_ch(), outputs the sampled version of the impulse response hmp(t) of the multipath fading channel as a vector. function impulse_response=ht_mp_ch(max_delay,L,decay_base,t_step) t_vector=0:t_step:max_delay; mp_tmp=0*(t_vector); path_delays=[0 sort(rand(1,L-1)*max_delay)]; impulse_positions=floor(path_delays/t_step); mp_tmp(impulse_positions+1)=exp(j*2*pi*rand(1,L)); mp_tmp=mp_tmp.*(decay_base.^(t_vector/max_delay)); impulse_response=mp_tmp/sqrt(sum(abs(mp_tmp).^2)); Explain what the variable on the left-hand side represents and justify how the right-hand side expression is formulated by adding comments to every line.
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a multiple 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, …
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more...
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, …
Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
42. You can prove in the following way that it is overall more efficient for an...
42. You can prove in the following way that it is overall more efficient for an engine to exhaust at the ambient environmental temperature than to exhaust at a cooler temperature that requires refrigeration. Suppose that the hot reservoir is at Th, the ambient temperature is Ta, and the refrigerated tem- perature is Tc. The engine cannot put out more work than would a Carnot engine operating between Th and Tc, so Weng = eQh = a[(Th − Tc)/Th] Qh,...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
The function f(x) = x^2 (x^2 − 2x − 36) is defined on all real numbers....
The function f(x) = x^2 (x^2 − 2x − 36) is defined on all real numbers. On what subset of the real numbers is f(x) concave down?
The revenue function for a company can be defined as; TR = Price (P) ´ Quantity...
The revenue function for a company can be defined as; TR = Price (P) ´ Quantity Demanded (Q). If the ordinary demand function for your firm is Q = 60 - 0.4P: a.   What is the total revenue function for this firm in terms of Q? b.   What is the average revenue function for this firm in terms of Q? c.   What is the MR function for this firm in terms of Q? d.   Show that MR will be less...
The revenue function for a company can be defined as; TR = Price (P) ´ Quantity...
The revenue function for a company can be defined as; TR = Price (P) ´ Quantity Demanded (Q). If the ordinary demand function for your firm is Q = 75 - 0.4P:    What is the total revenue function for this firm in terms of Q?    What is the average revenue function for this firm in terms of Q?    What is the MR function for this firm in terms of Q?    Show that MR will be less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT