167k views
5 votes
Look at square numbers one more time: square(1) = 1 square(N) = square(N-1) + 2N -1 Assume the definition has been implemented correctly. How many activations will there be on the activation chain if main() calls square(5)?

User Rsjethani
by
7.9k points

1 Answer

4 votes

Final answer:

When the function square(5) is invoked, there will be a total of 5 activations on the activation chain, each representing a call to itself with N-1 until it reaches the base case of square(1).

Step-by-step explanation:

The question ponders over the number of activations in an activation chain for a recursive implementation of a function to calculate the square of a number when the function square(5) is called.

We observe that the function definition square(N) = square(N-1) + 2N - 1 means that for each value of N, the function calls itself with N-1. Hence, for square(5), there will be a call to square(4), which will call square(3), and so on until it reaches the base case square(1).

Thus, the total number of activations when calling square(5) would be:

  • square(5)
  • square(4)
  • square(3)
  • square(2)
  • square(1)

That is a total of 5 activations on the activation chain.

User Bernd Ebertz
by
8.3k points