# Python programming implements the binary method and the Newton iteration method to obtain the square root code, and python square root

Source: Internet
Author: User

Python programming implements the binary method and the Newton iteration method to obtain the square root code, and python square root

Evaluate the square root function sqrt (int num) of a number, which is provided in most languages. How does one implement the square root of a number?
In fact, there are two main algorithms for finding the square root: binary search and Newton iteration)

1: Binary

Root number 5

A: Half: 5/2 = 2.5
B: Square verification: 2.5*2.5 = 6.25> 5, and the current upper limit is 2.5.
C: fold down again: 2.5/2 = 1.25
D: Square verification: 1.25*1.25 = 1.5625 <5. The current minimum value is 1.25.
E: half again: 2.5-(2.5-1.25)/2 = 1.875
F: Square verification: 1.875*1.875 = 3.515625 <5. The current minimum value is 1.875.

Compare the current value with 5, write down the lower limit and upper limit, iterate in sequence, and gradually approach the square root:

`import math from math import sqrt  def sqrt_binary(num):   x=sqrt(num)   y=num/2.0   low=0.0   up=num*1.0   count=1   while abs(y-x)>0.00000001:     print count,y     count+=1         if (y*y>num):       up=y       y=low+(y-low)/2     else:       low=y       y=up-(up-y)/2   return y  print(sqrt_binary(5)) print(sqrt(5)) `

Running result:
1 2.5
2 1.25
3 1.875
4 2.1875
5 2.34375
6 2.265625
7 2.2265625
8 2.24609375
9 2.236328125
10 2.2314453125
11 2.23388671875
12 2.23510742188
13 2.23571777344
14 2.23602294922
15 2.23617553711
16 2.23609924316
17 2.23606109619
18 2.23608016968
19 2.23607063293
20 2.23606586456
21 2.23606824875
22 2.23606705666
23 2.2360676527
24 2.23606795073
25 2.23606809974
26 2.23606802523
27 2.23606798798
2.23606796935
2.2360679775
[Finished in 0.1 s]

After 27 binary iterations, the difference between the obtained value and the system sqrt () is 0.00000001, and the accuracy is one,

0.001 8 iterations required

Therefore, in the case of low precision requirements, the bipartite method is also a relatively efficient algorithm.

2: Newton Iteration

After careful consideration, we can find that the problems we need to solve can be simply understood.

In the function sense, we need the function f (x) = x² to make the approximate solution of f (x) = num, that is, the approximate solution of x 2-num = 0.

From a geometric point of view: we require that g (x) = x 2-num be the closest point to the x axis intersection (g (x) = 0.

Let's assume that g (x0) = 0, that is, x0 is the positive solution, so what we need to do is to let the approximate x constantly approach x0, which is the definition of the function derivative:

You can obtain

From the geometric point of view, because the derivative is a tangent, through continuous iteration, the intersection of the derivative and the X axis will constantly approach x0.

For general cases:

Replace m = 2:

`def sqrt_newton(num):   x=sqrt(num)   y=num/2.0   count=1   while abs(y-x)>0.00000001:     print count,y     count+=1     y=((y*1.0)+(1.0*num)/y)/2.0000   return y  print(sqrt_newton(5)) print(sqrt(5)) `

Running result:
1 2.5
2 2.25
3 2.23611111111
2.23606797792
2.2360679775

Accurate to one of the hundreds of millions, the Newton method only iterates three times, 10 times that of the binary method.

3: Use the Newton method to obtain the open cube

`def cube_newton(num):   x=num/3.0   y=0   count=1   while abs(x-y)>0.00000001:     print count,x     count+=1     y=x     x=(2.0/3.0)*x+(num*1.0)/(x*x*3.0)   return x  print(cube_newton(27))  `

Calculus, probability, and line generation are basic courses for advanced algorithms. However, after so many years, I have forgotten that ..............................

Summary

The above is all about the implementation of the binary method and the Newton Iteration Method for the square root code in Python programming. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message.

Related Keywords:
Related Article

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.