Question

In: Computer Science

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 detail. We're going to use the concepts of binary search to try and approximate nding the square root and nding the nth root.

You may not use any library functions for these question, except DecimalFormat.

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 detail. We're going to use the concepts of binary search to try and approximate nding the square root and nding the nth root. You may not use any library functions for these question, except DecimalFormat.

Solutions

Expert Solution

  • Since there are no infomation the precision that the root must have, we will caluculate the root to 13 decimal places

Program plan:

Square root of n:

  • We will use binary search approach to calculate the sqaure root of n
  • intialize a variable low = 0 and another variable high to n.
  • declare a double variable root.
  • while low<high do the following:
    • Set a variable mid to (low+high )/2
    • if mid*mid is equal to n, then set root = mid and break from loop
    • if mid*mid < n then, the root must be greater than mid. Set low = mid+1
    • if mid*mid > n then, the root must be less than mid, Set high = mid-1
    • Set root = mid
  • Now we will caluculate the decimal part of the root to 13 decimal place.
  • declare a variable decimal = 0.1
  • loop 13 times:
    • while root*root <= n, increment root by decimal
    • divide decimal by 10 and store it to decimal
  • the resulting root value is the required root

pth root of n:

  • The steps to solve pth root of n follows the same steps as square root algorithm
  • Only change, is that we must calculate mid^p in the places we caluculate mid*mid and calculate root^p in the places we calculate root*root
  • We will use Math.pow() to calculate mid^p and root^p

Program with sample main():

import java.io.*;
import java.util.*;

class Roots
{
   static double root2(int n){
       int low = 0, high = n;
       int mid;
       double root = 0.0;
       while(low<high){
           mid = (low+high)/2;
           //System.out.println(low);
           //System.out.println(mid);
           //System.out.println(high);
           if(mid*mid==n){
               root = mid;
               break;
           }
           else if(mid*mid<n){
               low = mid+1;
           }
           else{
               high = mid-1;
           }
           root = mid;
       }
       double decimal = 0.1;
       int i = 0;
       while(i<13){
           while(root*root<=n){
               root += decimal;
           }
           root -= decimal;
           decimal /= 10;
           i+=1;
       }
       return root;
   }

   static double rootn(int n, int p){
       int low = 0, high = n;
       int mid;
       double root = 0.0;
       while(low<high){
           mid = (low+high)/2;
           //System.out.println(low);
           //System.out.println(mid);
           //System.out.println(high);
           if(Math.pow(mid,n)==n){
               root = mid;
               break;
           }
           else if(Math.pow(mid,n)<n){
               low = mid+1;
           }
           else{
               high = mid-1;
           }
           root = mid;
       }
       double decimal = 0.1;
       int i = 0;
       while(i<13){
           while(Math.pow(root,n)<=n){
               root += decimal;
           }
           root -= decimal;
           decimal /= 10;
           i+=1;
       }
       return root;

   }
   public static void main(String[] args){
       System.out.print("square root of 28: ");
       System.out.println(root2(28));
       System.out.print("5th root of 28: ");
       System.out.println(rootn(28,5));
   }
}

Sample output:


Related Solutions

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...
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...
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...
write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
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...
Graph the square root of the following function. State the domain and range of the square root of the function.
Graph the square root of the following function. State the domain and range of the square root of the function.y = -2x + 4
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.data spread; population standard deviationStandard deviation; data spreadpopulation standard deviation; variancevariance; standard deviation
(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...
The population standard deviation is the a. Square of the population variance b. square root of...
The population standard deviation is the a. Square of the population variance b. square root of the sum of the deviations from the mean c. same as the expected value d. positive square root of the population variance Variance is a measure of the average, or central value of a random variable. True False
What is the concept of the square root of time rule?
What is the concept of the square root of time rule?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT