Results 1 to 4 of 4
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Newton's Square Root Method, need somebody to help see the problem.

    Java Code:
    import java.util.Scanner;
    
    public class SquareRoot {
    
    	public static void main(String[] args) {
    
    		Scanner in = new Scanner(System.in);
    		System.out.println("Enter a value for (n)");
    		double n = in.nextDouble();
    		double x = 1, y = 1;
    		x = y;
    
    			while(Math.abs(x - y) <= 0.00001)
    			{
    				x=y;
    				x = ((n / x) + x) / 2.0;
    			}
    			
    			System.out.println("Newton("+n+") = "+x);
    			System.out.println("Math.sqrt("+n+") = "+Math.sqrt(n));
    	}
    }
    Here's my output.

    Enter a value for (n)
    4
    Newton(4.0) = 2.5
    Math.sqrt(4.0) = 2.0
    Where's the extra .5 in 2.5 coming from?
    Last edited by LetsG0Blue; 02-22-2013 at 04:14 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Newton's Square Root Method, need somebody to help see the problem.

    Well, initially, x-y == 0 which is less than .00001 so the loop goes thru once.

    Then x = 2.5 and since 2.5 -1 > .00001 the loop exits.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Newton's Square Root Method, need somebody to help see the problem.

    But if I put a > there it equals 1 for the output of Newton

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Newton's Square Root Method, need somebody to help see the problem.

    That is because you chose x and y to be the same values to start so the loop would never enter if x = y = 1. So you could either chose different values for x and y. Or just change to a do while loop to force at least one iteration of the loop.

    And I believe you want to set y = x.

    Jim
    Last edited by jim829; 02-22-2013 at 04:47 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. How to work out square root of Total
    By ls7897 in forum New To Java
    Replies: 23
    Last Post: 03-09-2011, 05:18 AM
  3. Find the square root with a particular method
    By roud9 in forum New To Java
    Replies: 2
    Last Post: 09-27-2010, 11:39 PM
  4. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 02:11 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •