GATE CSE
First time here? Checkout the FAQ!
x
0 votes
71 views

Consider the code below, defining the function $mystery:$

mystery(a,b){
    if (a < 0 or b < 0) return 0;
    else if (a == 0) return b+1;
    else if (b == 0) return mystery(a-1,1);
    else return mystery(a-1, mystery(a,b-1));
}
  1. Express $mystery(1, \:n)$ as a function of $n$.
  2. Express $mystery(2,\: n)$ as a function of $n$.
  3. Compute $mystery(3, \:2)$ and $mystery(3, 3)$.
asked in Algorithms by Veteran (76.3k points)   | 71 views

2 Answers

+1 vote
i) mystry(1,n)=n+2

ii)mystry(2,0)=3

mystry(2,1)=5

mystry(2,2)=7

.................

mystry(2,n)=2n+3

 

iii) mystry(1,1)=mystry(0,2)=3

as, mystry(1,0)=3

mystry(0,1)=2

 

mystery(1,2)=mystery(0,mystery(1,1))=mystery(0,3)=4

mystery(2,1)=mystery(1,mystery(2,0))=mystery(1,3)=5

 

mystery(1,3)=5

 

mystery(3,0)=mystery(2,1)=5

 

mystery(3,1)=mystery(2,mystery(3,0))=mystery(2,5)=13

As, mystery(2,5)=mystery(1,mystery(2,4))=13

mystery(2,4)=mystery(1,mystery(2,3))=11

mystery(2,3)=mystery(1,mystery(2,2))=9

mystery(2,2)=mystery(1,mystery(2,1))=7

 

mystery(3,2)=mystery(2,mystery(3,1))=mystery(2,13)=29 [By putting above formula 2n+3 we also can get it]
answered by Veteran (53.2k points)  
0 votes

i. mystery(1,n) = n+2

ii. mystery(2,n)=2n + 3

iii. mystery(3,2)= 29 and mystery(3,3)= 61

 

 

answered by Veteran (24.6k points)  

Related questions



Top Users Apr 2017
  1. akash.dinkar12

    3660 Points

  2. Divya Bharti

    2580 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Debashish Deka

    1614 Points

  7. Shubham Sharma 2

    1610 Points

  8. Prashant.

    1492 Points

  9. Arjun

    1472 Points

  10. Arunav Khare

    1464 Points

Monthly Topper: Rs. 500 gift card

22,088 questions
28,063 answers
63,298 comments
24,173 users