Question: (1) (100 points) In this problem, we will implement a square root approxi- mater and then an nth root approximater. Recall that the nth root

(1) (100 points) In this problem, we will implement a square root approxi- mater and then an nth root approximater. Recall that the nth root of x, written nx, is the number when raised to the power n gives x. We learned in class that we can use the concepts of binary search to approx- imate the square root of a number, and we can continue this logic to approximate the nth square root. Please look at Lecture Notes 03, section 4 for a little more detail. Were going to use the concepts of binary search to try and approximate finding the square root and finding the nth root. You may not use any library functions for these question, except DecimalFormat.

  1. (a) Please fill in the squareRootFinder(int number, int iterations) method. The method should return a string representing an approx- imation of the square root of the number to 5 decimal places.

    For example: squareRootFinder(9, 2) means that we want to approximate the square root of 9 with two iterations of binary search. We know that the square root of 9 must be between 0 and 9. Thus, lets let the ranges that we look at be (0,9). We see the middle of that to be 4.5. (4.5)2 is equal to 20.25. 20.25 is too high because its greater than 9. Thus, we now set our ranges were looking at to be (0, 4.5). The middle is now 2.25. (2.25)2 is equal to 5.0625, which is now too low since its less than 9. We did 2 iterations. Thus, our best guess for the square root is 2.25000 (remember, 5 decimal point accuracy). If we were doing 3 iterations, wed now set our new range to (2.25, 4.5).

  2. (b) Please fill in the nthRootFinder(int number, int iterations, int n). Now were approximating the nth root of our number. Recall that the nth root of x, written nx, is the number when raised to the power n gives x. For example, the cubed root of 64 is 364 which is equal to 4, since 43 = 64. Youll do the same binary search method as described in part (a) for squareRootFinder, but now youre check- ing to see if its the nth root.

For example: nthRootFinder(64, 1, 3) is trying to approximate the cubed root of 64 with only one iteration of binary search. Hint: the code for nthRootFinder looks just like the code for squareRootFinder except maybe 3 more lines of code. Remember, you may not use any library functions for these questions, except DecimalFormat.

(1) (100 points) In this problem, we will implement a square rootapproxi- mater and then an nth root approximater. Recall that the nth

import java.text.DecimalFormat;

class Main { public static void main(String[] args) { /* when submitting, please don't leave anything in the main method. for testing, you can uncomment the lines below and fill in the variables with the variables you want to test: */ // String result1 = squareRootFinder(number, iterations); // System.out.println(result1); // String result2 = nthRootFinder(numer, iterations, n); // System.out.println(result2); } public static String squareRootFinder(int number, int iterations){ // TODO: implement the squareRootFinder here return ""; } public static String nthRootFinder(int number, int iterations, int n){ // TODO: implement the nthRoofFinder here return ""; } }

(1) (100 points) In this problem, we will implement a square root approxi- mater and then an nth root approximater. Recall that the nth root of X, written "x, is the number when raised to the power n gives x. We learned in class that we can use the concepts of binary search to approx- imate the square root of a number, and we can continue this logic to approximate the nth square root. Please look at Lecture Notes 03, section 4 for a little more detail. We're going to use the concepts of binary search to try and approximate finding the square root and finding the nth root. You may not use any library functions for these question, except DecimalFormat. (a) Please fill in the squareRootFinder(int number, int iterations) method. The method should return a string representing an approx- imation of the square root of the number to 5 decimal places. For example: squareRootFinder(9, 2) means that we want to approximate the square root of 9 with two iterations of binary search. We know that the square root of 9 must be between 0 and 9. Thus, let's let the ranges that we look at be (0,9). We see the middle of that to be 4.5. (4.5)2 is equal to 20.25. 20.25 is too high because it's greater than 9. Thus, we now set our ranges we're looking at to be (0, 4.5). The middle is now 2.25. (2.25)2 is equal to 5.0625, which is now too low since it's less than 9. We did 2 iterations. Thus, our best guess for the square root is 2.25000 (remember, 5 decimal point accuracy). If we were doing 3 iterations, we'd now set our new range to (2.25, 4.5). (b) Please fill in the nthRootFinder(int number, int iterations, int n). Now we're approximating the nth root of our number. Recall that the nth root of x, written x, is the number when raised to the power n gives x. For example, the cubed root of 64 is 3/64 which is equal to 4, since 43 = 64. You'll do the same binary search method as described in part (a) for squareRootFinder, but now you're check- ing to see if it's the nth root. For example: nthRootFinder(64, 1, 3) is trying to approximate the cubed root of 64 with only one iteration of binary search. Hint: the code for nthRootFinder looks just like the code for squareRootFinder except maybe 3 more lines of code. Remember, you may not use any library functions for these questions, except DecimalFormat

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!