→This problem is based on Recursion. (a part of data structure).
Let J and K be integers and suppose Q(J,K) is recursively defined by
Q(J,K)={5 ifJ<K
{ and Q (J-K,K+2)+J if J>=K
Find Q(2,7),Q(5,3) and Q(15,2)
Answers
Answer:
Q(2,7)=5
Q(5,3)=10
Q(15,2)=42
Answer: We will use the recursive definition of Q(J,K) to compute the values of Q(2,7), Q(5,3), and Q(15,2).
For Q(2,7), we have J < K, so Q(2,7) = 5.
For Q(5,3), we have J >= K, so we must use the second case of the recursive definition. We get:
Q(5,3) = Q(5-3, 3+2) + 5 = Q(2, 5) + 5
Now, we need to use the first case of the recursive definition, since 2 < 5. Therefore:
Q(2,5) = 5
Putting it all together, we get:
Q(5,3) = Q(2,5) + 5 = 5 + 5 = 10
For Q(15,2), we have J >= K, so we must use the second case of the recursive definition. We get:
Q(15,2) = Q(15-2, 2+2) + 15 = Q(13, 4) + 15
Now, we need to use the second case of the recursive definition again, since 13 >= 4. Therefore:
Q(13,4) = Q(13-4, 4+2) + 13 = Q(9,6) + 13
Using the second case of the recursive definition again, we get:
Q(9,6) = Q(9-6, 6+2) + 9 = Q(3,8) + 9
Since 3 < 8, we must use the first case of the recursive definition. Therefore:
Q(3,8) = 5
Putting it all together, we get:
Q(15,2) = Q(13, 4) + 15 = (Q(9,6) + 13) + 15 = ((Q(3,8) + 9) + 13) + 15 = 5 + 9 + 13 + 15 = 42
Therefore, Q(2,7) = 5, Q(5,3) = 10, and Q(15,2) = 42.