Question

What is the output of the following java function when called as f(1,25,25)? Int f(int a,...

What is the output of the following java function when called as f(1,25,25)?


Int f(int a, int b, int n){
int mid =(a+b)/2;
if ((mid*mid <=n) && (n< (mid+1)*(mid+1))
return mid;
else If (mid*mid>n)
return f(a,mid-1,n);
else
return f(mid+1,b,n);
}

B.)

Is the following function tail-recursive?

Acker(m,n){
if(m=0)
n+1
else if(n=0)
Acker(m-1,1)
else
Acker(m-1,Acker(m,n-1))
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here are the answers..

==================================================================

Program:

==================================================================

import java.io.*;

public class Test
{
   public static void main (String[] args)
   {
       System.out.println(f(1, 25, 25));
   }
  
public static int f(int a, int b, int n)
{
  
int mid =(a+b)/2;
if ((mid*mid <=n) && (n< (mid+1)*(mid+1)))
return mid;
else if (mid*mid>n)
return f(a,mid-1,n);
else
return f(mid+1,b,n);
}
}

==================================================================

Output = 5

==================================================================

Ans: B

Acker(m,n){
if(m=0)
n+1
else if(n=0)
Acker(m-1,1)
else
Acker(m-1,Acker(m,n-1))
}

The given function is tail recursive.

Explanation:

A recursive function is tail recursive when recursive call is the last thing executed by the function.

so in the given function Acker(m-1,Acker(m,n-1)) call is the last statement executed by the function. Hence it's a tail recursive function.

==================================================================

Kindly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
What is the output of the following java function when called as f(1,25,25)? Int f(int a,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT