The function t is defined recursively as follows tn Write

The function t( ) is defined recursively as follows t(n) = Write, in pseudocode, an iterative algorithm for this function. Your function must take as input the single number and return as output the vector t=t(1), t(2), , t(n)]. Be sure to declare your function with an appropriate name. Trace your algorithm for the input = 5.

Solution

Python script is as follows: def func(int n): if n == 1: t = [0] return(t) elif n == 2: t = [0, 1] return(t) else: t = func(n - 1) tn = t[len(t) - 2] + 2 * t[len(t) - 1] - n + 1 t.append(tn) return(t) When n = 5, the function is run as func(5) which runs else loop and calls func(4), which calls func(3), which then finally calls func(2) which returns [0,1] as t. In func(3), tn = t[0] + 2*t[1] - 3 + 1 = 0, which is appended to the array t and returned to func(4) as [0,1,0] where tn = t[1] + 2*t[2] - 4 + 1 = -2 which is appended to t in func(5). Here t = [0,1,0,-2] and tn = t[2] + 2*t[3] - 5 + 1 = -8 which is finally appended to t to form [0,1,0,-2,-8] which is returned back.
 The function t( ) is defined recursively as follows t(n) = Write, in pseudocode, an iterative algorithm for this function. Your function must take as input the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site