Go dichotomy and Newton iterative method to find square root

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Two-part method

    • Seeking Radical 5
    • Binary: 5/2=2.5
    • Squared checksum: 2.5*2.5=6.25>5, and gets the current limit of 2.5
    • Binary down again: 2.5/2=1.25
    • Square Check: 1.25*1.25=1.5625<5, get current lower limit 1.25
    • Again binary: 2.5-(2.5-1.25)/2=1.875
    • Square Check: 1.875*1.875=3.515625<5, get current lower limit 1.875

Newton Iterative Method


The function f (x) = x ² can be understood so that the approximate solution of f (x) = num is the approximate solution of x²-num = 0.

Code

123456789101112131415161718192021222324252627282930313233343536373839404142434445
 PackageMainImport("FMT"    "Math") func newtonsqrt(num float64) float64 {x: = num/2.0    varYfloat64=0Count: =1     forMath. Abs (x-y) >0.00000001{//FMT. Println (count, x)Count + =1y = x x = (1.0/2.0) *x+ (num*1.0)/(x*2.0)    }returnX func binarysqrt(num float64) float64 {y: = num/2.0Low: =0.0Up: = num Count: =1     forMath. Abs (Y * y-num) >0.00000001{//FMT. Println (count, y)Count + =1        ifY*y > num{up = y y = low+ (y-low)/2}Else{low = y y = up-(up-y)/2}    }returnY func main() {FMT. Println ("Math sqrt", Math. SQRT (5) FMT. Println ("Newton sqrt", Newtonsqrt (5) FMT. Println ("binary sqrt", Binarysqrt (5))}

Accuracy 0.00000001, in one-;
The dichotomy method has been iterated through 27 iterations;
Newton's method only iterates 3 times, is 10 times times the dichotomy method;
System sqrt is the fastest and most accurate, do not know the adoption of what principle to achieve?

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.