Java-to find the radical n

Source: Internet
Author: User
Tags square root

Square, the open root is very simple in Java, math.sqrt (double n) or Math.pow (double A, double b), the B-side of a. But we can think for ourselves how exactly these methods are implemented.

Take the square root to explain, it has two methods, dichotomy and Newton iterative method.

Two-part method:

Like the radical 5.

First step: binary: 5/2=2.5

The second step: Square check: 2.5*2.5=6.25>5, and gets the current upper limit of 2.5, recorded.

Step three: Binary down again: 2.5/2=1.25

Fourth step: Square check: 1.25*1.25=1.5625<5, get current lower 1.25, record

Fifth step: Binary again: 2.5-(2.5-1.25)/2=1.875

Sixth step: Square Check: 1.875*1.875=3.515625<5, get the current lower limit 1.875, replace the lower value

......

Until the difference between 5 and the margin is within the range of the error you define, the loop ends.

Code:

ImportJava.text.DecimalFormat; Public classMain { Public Static DoublesqrtDoublenum) {        if(num<0) {            return-1; }                DoubleLow = 0; DoubleHigh = NUM/2; DoublePrecision = 0.000001; format, guaranteed output bits DecimalFormat df=NewDecimalFormat ("#.00"); Doubleres =High ;  while(Math.Abs (num-(res*res)) >Precision) {            if(High*high >num) {                DoubleN= High-(high-low)/2; if(n*n>num) { High=N; } Else if(n*n<num) { Low=N; }Else {                    returndouble.valueof (Df.format (n)); } Res=N; } Else if(High*high <num) {                Doublem = high + (high-low)/2; if(m*m>num) { Low=High ; High=m; } Else if(m*m<num) { Low=High ; High=m; }Else {                    returndouble.valueof (Df.format (m)); } Res=m; } Else {                returndouble.valueof (Df.format (high)); }        }                returndouble.valueof (Df.format (res)); }         Public Static voidMain (string[] args) {DoubleA = 7; System.out.println (sqrt (37)); }}

Newton Iterative Method:

Actually is the thought of approximation, for example we require the square root of a, first make f (x) =x^2-a, then our goal is to get X to make f (x) = 0, that is, to find x^2-a this curve and x-axis intersection, drawing example:

By the function f (x) =x^2-a, we can know that the slope of the tangent of any point (x, y) on the function is 2x. Assuming that the tangent equation for the over point (X0,Y0) is y=kx+b, then the intersection of the tangent and X axis is-b/k. and b=y0-kx0,k=2x0,y0=x0^2-a, simplifying-b/k= (x0+a/x0)/2.

That is to say (x0+a/x0)/2 is the horizontal axis of the tangent of the crossing point (X0,y0) and the intersection of the X axis. Remember (x0+a/x0)/2=x ', continue to ask for a point (X ', F (x ')) tangent to the x axis of the intersection of the horizontal X ", it is clear that X ' is closer to the function f (x) =x^2-a and x-axis intersection of the horizontal axis (that is, the positive square root of a). The gradual approximation f (x) = 0;

So the formula is: X ' = (x ' +a/x ')/2.

Code:

ImportJava.text.DecimalFormat; Public classMain1 { Public Static DoublesqrtDoublex) {if(x<0) {            return-1; }        //format, guaranteed output number of bitsDecimalFormat DF =NewDecimalFormat ("#.00"); DoubleK =x; DoublePrecision = 0.000001;  while((K*k-x) >Precision) {k=0.5* (k+x/k); }        returndouble.valueof (Df.format (k)); }         Public Static voidMain (string[] args) {DoubleA = 9;    System.out.println (sqrt (a)); }}

Reference documents:

Square root by dichotomy and Newton iterative method (Python implementation)

Newton's iterative method to find square root

Java-to find the radical n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.