wiley_pastebin

Question 16

Apr 11th, 2019
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. public class AproxSquareRoot{
  2.     public static void main(String[] args){
  3.         AproxSquareRoot asr = new AproxSquareRoot();
  4.         System.out.println(asr.calculateFor(125, 5));
  5.     }
  6.     public double calculateFor(int number, int accuracy){
  7.         String doubleAsString = Integer.toString(number);
  8.         int doubleLength = doubleAsString.length();
  9.         double x = number * doubleLength;
  10.         int i = 0;
  11.         while(i <= accuracy){
  12.             x = 0.5 * (x + (number/x));
  13.             i++;
  14.         }
  15.         return x;
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment