Question

In: Computer Science

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 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

Solutions

Expert Solution

code:


/**
*
* @author VISHAL
*/

class NthRootClass
{
     
   static String findNthRoot(int number, int n,int precision)
   {
      
       // intially guessing a random number between
       // 0 and 9
       double xPre = Math.random() % 10;
   double eps=1.0;
double p=1.0;
       // smaller eps, denotes more accuracy
       for(int i=1; i<=precision; i++)
{
eps/=10;
p*=10;
}

  
       // initializing difference between two
       // roots by INT_MAX
       double delX = 2147483647;
  
       // xK denotes current value of x
       double xK = 0.0;
  
       while (delX > eps)
       {
      
           xK = ((n - 1.0) * xPre +
           (double)number / Math.pow(xPre, n - 1)) / (double)n;
           delX = Math.abs(xK - xPre);
           xPre = xK;
       }
double scale=Math.pow(10,precision);
       xK=Math.round(xK*scale)/scale;
return String.valueOf(xK);
   }
   public static void main (String[] args)
   {
       System.out.println("Nth root is "   + findNthRoot(47, 2,2));

   }
}


Related Solutions

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...
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...
An element a in a field F is called a primitive nth root of unity if...
An element a in a field F is called a primitive nth root of unity if n is the smallest positive integer such that an=1. For example, i is a primitive 4th root of unity in C, whereas -1 is not a primitive 4th root of unity (even though (-1)4=1). (a) Find all primitive 4th roots of unity in F5 (b) Find all primitive 3rd roots of unity in F7 (c) Find all primitive 6th roots of unity in F7...
(a) Let n be odd and ω a primitive nth root of 1 (means that its...
(a) Let n be odd and ω a primitive nth root of 1 (means that its order is n). Show this implies that −ω is a primitive 2nth root of 1. Prove the converse: Let n be odd and ω a primitive 2nth root of 1. Show −ω is a primitive nth root of 1. (b) Recall that the nth cyclotomic polynomial is defined as Φn(x) = Y gcd(k,n)=1 (x−ωk) where k ranges over 1,...,n−1 and ωk = e2πik/n is...
Prove that for an nth order differential equation whose auxiliary equation has a repeated complex root...
Prove that for an nth order differential equation whose auxiliary equation has a repeated complex root a+bi of multiplicity k then its conjugate is also a root of multiplicity k and that the general solution of the corresponding differential equation contains a linear combination of the 2k linearly independent solutions e^(ax)cos(bx), xe^(ax)cos(bx),  x^2e^(ax)cos(bx),...,  x^(k-1)e^(ax)cos(bx) e^(ax)sin(bx), xe^(ax)sin(bx), x^2e^(ax)sin(bx),..., x^(k-1)e^(ax)sin(bx)
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis...
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis of the Algorithme lecture
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
Recall that a∈Z/nZ is called a primitive root modulon, if the order of a in Z/nZ...
Recall that a∈Z/nZ is called a primitive root modulon, if the order of a in Z/nZ is equal to φ(n). We have seen in class that, if p is a prime, then we can always find primitive roots modulop.Find all elements of (Z/11Z)∗ that are primitive roots modulo 11.
Implement a function named printRange that, given the pointer to the root of a BST, a...
Implement a function named printRange that, given the pointer to the root of a BST, a low key value, and a high key value, prints in sorted order all records whose key values fall between the two given keys (inclusive). Function printRange should visit as few nodes in the BST as possible. Here is the start code (in Java 8) for this problem. Input Format Three lines. The first line includes the number of keys to be put in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT