CS13002 Programming and Data Structures, Spring 2002--2003
(SECTION 1/A)

LAB TEST 2

For students with odd PC numbers

Consider three sequences of integers defined recursively as follows:

A0 = 0
A1 = 1
An = A[n/3] - 2Bn-2 + Cn  for n >= 2

B0 = -1
B1 = 0
B2 = 1
Bn = n - An-1 + Cn-2 - Bn-3  for n >= 3

C0 = 1
Cn = Bn - 3C[n/2] + 5  for n >= 1
Here for a real number x the notation [x] stands for the largest integer less than or equal to x. For example, [3]=3, [3.1416]=3, [0.1416]=0, [-1.1416]=-2, [-3]=-3, [5/3]=1, [-5/3]=-2, etc. For this exercise you need consider x>=0 only, in which case [x] can be viewed as the integral part of x.


Part I

Write three mutually recursive functions for computing An, Bn and Cn.

Compute A25. Count the total number of times Ai, Bi and Ci are computed for i=0,...,25 (that is, the corresponding functions are called with argument i) during the computation of A25.

Compute B25. Count the total number of times Ai, Bi and Ci are computed for i=0,...,25 during the computation of B25.

Compute C25. Count the total number of times Ai, Bi and Ci are computed for i=0,...,25 during the computation of C25.

Part II

Part I illustrates that the functions are called multiple times on the same argument. The number of calls in some cases is intolerably large. This can be remedied by avoiding recursion as follows.

Write an iterative version of the mutually recursive procedure. Maintain three arrays a, b and c each of size 26. Use the boundary conditions (values for A0, A1, B0 etc.) to initialize. Then use the recursive definition to update the A, B and C values ``simultaneously''. In this method if some value (say, Ai) is once computed, it is stored in the appropriate array location for all subsequent uses. This saves the time for recalculating the same value again and again.

Compute the values A25, B25 and C25 using the iterative procedure.


Typical output

The recommended format of the output for computing A10, B10 and C10 is as follows:

Recursive method :
A(10)=-28
Number of calls:
A( 0):     121 A( 1):      87 A( 2):     121 A( 3):      52 A( 4):      24
A( 5):      11 A( 6):       6 A( 7):       3 A( 8):       1 A( 9):       1
A(10):       1
B( 0):     232 B( 1):     529 B( 2):     275 B( 3):     111 B( 4):      50
B( 5):      24 B( 6):      11 B( 7):       6 B( 8):       3 B( 9):       1
B(10):       1
C( 0):     427 C( 1):     427 C( 2):     227 C( 3):      89 C( 4):      38
C( 5):      18 C( 6):       9 C( 7):       4 C( 8):       2 C( 9):       1
C(10):       1
B(10)=-5
Number of calls:
A( 0):      95 A( 1):      68 A( 2):      95 A( 3):      41 A( 4):      18
A( 5):       9 A( 6):       5 A( 7):       2 A( 8):       1 A( 9):       1
A(10):       0
B( 0):     182 B( 1):     416 B( 2):     215 B( 3):      87 B( 4):      40
B( 5):      18 B( 6):       9 B( 7):       5 B( 8):       2 B( 9):       1
B(10):       1
C( 0):     335 C( 1):     335 C( 2):     179 C( 3):      69 C( 4):      30
C( 5):      14 C( 6):       7 C( 7):       3 C( 8):       2 C( 9):       1
C(10):       0
C(10)=-24
Number of calls:
A( 0):      97 A( 1):      70 A( 2):      97 A( 3):      42 A( 4):      19
A( 5):       9 A( 6):       5 A( 7):       2 A( 8):       1 A( 9):       1
A(10):       0
B( 0):     186 B( 1):     427 B( 2):     222 B( 3):      89 B( 4):      41
B( 5):      19 B( 6):       9 B( 7):       5 B( 8):       2 B( 9):       1
B(10):       1
C( 0):     344 C( 1):     344 C( 2):     184 C( 3):      71 C( 4):      31
C( 5):      15 C( 6):       7 C( 7):       3 C( 8):       2 C( 9):       1
C(10):       1

Iterative method :
a(10)=-28, b(10)=-5, c(10)=-24.


[Course home] [Home]