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?