Question

In: Computer Science

PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root...

PLEASE DO NOT CHANGE THE CODE BELOW!

In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may not use any library functions for this question.

In this question, our program expects 3 lines, where the first line is n (the degree of root to take), the second line is a number, and the third number represents the precision. The output is a single line containing the answer followed by a newline.

For example, the following input would generate the output 20.000: 2 400 3 Additionally, the following input would generate the output 6.86: 2

47

2

import java.util.Scanner;

public class NthRootFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// Read n (for taking the nth root)
int n = Integer.parseInt(sc.nextLine());
  
// Read number to take the nth root of
int number = Integer.parseInt(sc.nextLine());
  
// Read the desired precision
int precision = Integer.parseInt(sc.nextLine());
  
// Print the answer
System.out.println(findNthRoot(number, n, precision));
}
  
private static String findNthRoot(int number, int n, int precision) {
// TODO complete this method
double
return 0;
}
}

Solutions

Expert Solution

import java.util.Scanner;

public class NthRootFinder {
    
    public static double myPow(double x, double n) {
        if(n == 0) {
            return 1;
        }
        if(n > 0) {
            return x * myPow(x, n-1);
        } else {
            return myPow(x, n + 1) * 1.0/x;
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read n (for taking the nth root)
        int n = Integer.parseInt(sc.nextLine());

        // Read number to take the nth root of
        int number = Integer.parseInt(sc.nextLine());

        // Read the desired precision
        int precision = Integer.parseInt(sc.nextLine());

        // Print the answer
        System.out.println(findNthRoot(number, n, precision));
    }

    private static String findNthRoot(int number, int n, int precision) {

        double xPrevious = (double)number/n;  // just to start with a value
      
        double delX = Double.MAX_VALUE;  // for difference in actual and assumed value
      
        //  xK denotes current value of x 
        double xCurrent = xPrevious; 
      
        //  loop untill we reach desired accuracy 
        while (delX > myPow(10, -precision)) 
        {
            //  calculating root approximation newton's method 
            xCurrent = ((n - 1.0) * xPrevious + 
                  (double)number/myPow(xPrevious, n-1)) / (double)n; 
            delX = xCurrent - xPrevious;
            
            if(delX < 0) {
                delX *= -1;
            }
            
            xPrevious = xCurrent; 
        } 
      
        return String.format("%." + precision + "f", xPrevious); 
    }
}

Related Solutions

In this problem, we will implement an nth root finder. Recall that the nth root of...
In this problem, we will implement an nth root finder. Recall that the nth root of x, written n√ x, is the number when raised to the power n gives x. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal...
In this assignment, we will implement a square root approximater and then an nth root approximater....
In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of 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...
In C++ In this assignment, we will implement a square root approximater and then an nth...
In C++ In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We can use the concepts of binary search to approximate the square root of a number, and we can continue this logic to approximate the nth square root. We're going to use the concepts of binary search to try and approximate ending the square...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
Implement the following function by replacing the pass keyword with your code. Do not change the...
Implement the following function by replacing the pass keyword with your code. Do not change the spelling of the function name or the parameter list. def order_pie(): Important notes: Ask ONLY these questions, in EXACTLY this order. Do not ask more questions than are necessary to arrive at the right answer. You can expect the input values to be 'yes' or 'no'. For this assignment you do not have to worry about other spellings or capitalization. The return values must...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
This is in Python: Alter your code for previous Do It Now problem, with the change...
This is in Python: Alter your code for previous Do It Now problem, with the change that the row should only be printed by the second number that the user enters. For example, the user enters two numbers of 5 and 7 for the two input lines in the code, and the following will be printed: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT