Question

In: Computer Science

3. What does mystery2 compute on an input n? Please explain how you found this. thanks!...

3. What does mystery2 compute on an input n? Please explain how you found this. thanks!

public static int problem1(int n) {

int begin = 0;

int end = n+1;

while (begin+1 < end){

int mid = (begin+end)/2;

if(112*mid <= n){

begin = mid;

}

else{

end = mid;

}

}

return begin;

}

Solutions

Expert Solution

ANSWER: I created a program to test your method we have put n value is higher to get the result greater than 0 because this method always returning begin value which 0 and it is updating when if condition becomes true. below is the explanation in the code please like it.

CODE:

public class Test

{
   public static int problem1(int n) {

       int begin = 0;// here begin is initialized with 0

       int end = n+1; // end vaue will be 1 more than n

       while (begin+1 < end){ // this while will start from 1 to end means n+1

       int mid = (begin+end)/2; // mid value for each iteration

       if(112*mid <= n){ // condition which will be true if mid*112 > n then only begin will be equal to mid

       begin = mid;

       }

       else{

       end = mid; // otherwise end will be mid

       }

       }

       return begin; // returning the begin value always

       }
public static void main(String [] args)
  
{
   System.out.println("problem1 value when n=500: "+problem1(500));


   }

  

}

output:


Related Solutions

What is the Elmore delay of an n-input NAND gate? Please explain in detail and either...
What is the Elmore delay of an n-input NAND gate? Please explain in detail and either type answer or write with organized and clear handwriting.
Please explain your work. Thanks. Compute the probability of each of the following poker hands occurring...
Please explain your work. Thanks. Compute the probability of each of the following poker hands occurring (poker hand = 5 cards dealt out of a regular 52-card deck, the order does not matter). For the following explanations, let the letters u, v, w, x, y, z indicate face values (i.e. ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king). Please note that aces can be either high (that is, they come after the king sequentially) or...
Please do not just give the answer. Please also explain how you got them. Thanks! 1....
Please do not just give the answer. Please also explain how you got them. Thanks! 1. Which of the following is not a valid method of applying LCNRV: A. logical categories of inventory (i.e. product line) B. the entire inventory C. inventory items to be sold within the next year D. individual inventory items E. None of the answer choices are correct 2. Which of the following would not require the company to account for the change retrospectively? A. From...
Q.Can you please explain what is liquidation and how it affects the different stakeholders, how does...
Q.Can you please explain what is liquidation and how it affects the different stakeholders, how does a company manage a good liquidating position? (500 words)
Hi there, please write code in Python 3 and show what input you used for the...
Hi there, please write code in Python 3 and show what input you used for the program. I've been stuck on this for hours! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here;...
How does a fixed input differ from a variable input?What does a cost function show?What is...
How does a fixed input differ from a variable input?What does a cost function show?What is marginal cost?Is the marginal cost likely to be constant regardless of the level of output produced?What is the relationship between a firm’s marginal cost curve and its output supply curve? ( Please type it,thanks)
Describe what production-related risks are? please explain in detail and please no handwriting. Thanks
Describe what production-related risks are? please explain in detail and please no handwriting. Thanks
How to compute Optimal Portfolio Selection ? Explain in details, please
How to compute Optimal Portfolio Selection ? Explain in details, please
Could you please solve these problems and explain the steps? Consider a 3-input function G whose...
Could you please solve these problems and explain the steps? Consider a 3-input function G whose POS form is known to contain the maxterm X + Y + Z. 1) What is the maximum number of minterms that the SOP form of G can have such that no simplification is possible? Derive the corresponding Boolean expression for both SOP form and POS form. Can G0 be simplified? 2) Let us assume that all minterms that do not exist in the...
May someone explain (how to drow isomers for coordination)? bacouse I found it so difficult thanks
May someone explain (how to drow isomers for coordination)? bacouse I found it so difficult thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT