Python里面有内置(Built-in)的平方根函数:sqrt(),可以方便计算正数的平方根。那么,如果要自己定义一个sqrt函数,该怎么解决呢?

解决思路:

 1. 大于等于1的正数n的方根,范围肯定在0~n之间;小于1的正数n的方根,范围肯定在0~1之间

 2. 用二分法(Bisection method, Binary search)从中间开始找n的方根。

 3. 对于大于等于1的正数n,先假设n/2是n的方根,如果n/2的平方大于n,那么说明n的方根在0~n/2之间;如果n/2的平方小于n,说明n的方根在n/2~n之间。以此类推。。

 4. 对于小于1的正数n,先假设0.5是n的方根,方法同上

这样做的好处是,每次都可以去掉一半可能的值。因此,搜索的范围越来越小。

I------------------------I-------------------------I

0                          n/2                            n

举例来说,如果是求8的平方根,那么先假设8的平方根是4;

4的平方是16,16大于8,因此8的平方根范围缩小到0~4之间;

继续假设8的平方根是2,2的平方是4,4小于8,因此8的平方根范围缩小到2~4之间;

继续假设8的平方根是3,3的平方是9,9大于8,因此8的平方根范围缩小到2~3之间;

以此类推。。。

代码如下:

def sqrt_bi(n):
'''为了方便起见,先假设n为正数'''
low=0 #设置下限为0
high=max(n,1) #设置上限为n和1之中的最大数,即:如果n>=1,那么上限为n;如果n<1,那么上限为1
guess=(low+high)/2 #先从中间值开始猜
count=1 #设置猜测次数起始值为1
while abs(guess**2-n)>0.00000000000000000001 and count<100: #当猜测值的平方和n本身的差值无限接近误差值时,循环才会停止;同时设置猜测次数不超过100次
if guess**2<n: #如果猜测值的平方小于n,那么将此设为下限
low=guess
else: #如果猜测值的平方大于n,那么将此设为上限
high=guess
guess=(low+high)/2 #根据新的上下限,重新进行猜测
count+=1 #猜测次数每次增加1
return guess
* 这里,我将0.00000000000000000001设为epsilon(误差值,epsilon为接近0值的浮点数)。epsilon越接近0,算出的方根值就越精确。

调用此函数试一下,同时与python自带的sqrt函数进行对比:

print(sqrt_bi(8))
import math
print(math.sqrt(8))

运行结果如下:

2.82842712474619
2.8284271247461903

python自带的sqrt函数比sqrt_bi函数还要更精确一些。

参考:麻省理工学院公开课:计算机科学及编程导论 (第5课)

用二分法定义平方根函数(Bisection method Square Root Python)的更多相关文章

  1. 用牛顿-拉弗森法定义平方根函数(Newton-Raphson method Square Root Python)

    牛顿法(Newton’s method)又称为牛顿-拉弗森法(Newton-Raphson method),是一种近似求解实数方程式的方法.(注:Joseph Raphson在1690年出版的< ...

  2. CodeChef - SQRGOOD:Simplify the Square Root (求第N个含平方因子数)

    Tiny Wong the chef used to be a mathematics teacher in a senior high school. At that time, he always ...

  3. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Project Euler 80:Square root digital expansion 平方根数字展开

    Square root digital expansion It is well known that if the square root of a natural number is not an ...

  5. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

  6. Plus and Square Root

    ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ...

  7. Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))

    C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

  9. Square Root

    Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...

随机推荐

  1. MySQL的log_bin和sql_log_bin 的区别

    利用二进制还原数据库的时候,突然有点纠结,log_bin和sql_log_bin有什么区别呢?行吧,搜搜,结合自己的经验,简单说一下.log_bin:二进制日志. 在 mysql 启动时,通过命令行或 ...

  2. CONTINUE...?模拟分情况

    CONTINUE...? DreamGrid has  classmates numbered from  to . Some of them are boys and the others are ...

  3. 网络编程-TCP/IP

    TCP/IP五层模型讲解(2分) 我们将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议 就理解了整个互联网通信的原理. 首先,用户感知到的 ...

  4. Linux安装Apache常见报错(一)

    启动Apache提示报错:Could not reliably determine the server's fully qualified domain name, using localhost, ...

  5. 通过C#调用,实现js加密代码的反混淆,并运行js函数

    前一篇我测试了vba调用htmlfile做反混淆,并执行js加密函数的代码.本文换成C#实现. 联系QQ:564955427 C#操作JS函数,可以通过ScriptControl组件,但这个组件只能在 ...

  6. p68理想的性质

    1.如何由2.2.4推出后面的结论? 2.为什么A可以等于R? 3.如何证明3? π:R->R/M套用定理2.2.4(2)和(1) R2是R/M,I是R/M的理想也就是R2的理想,所以f^(-1 ...

  7. eclipse中不能保存汉字的解决方法

    首先分清是打开jsp页面的问题还是java文件的问题?    对于java文件,只要在你的项目上点击右键选择“Propertise”(属性)然后点击“Info”标签将里面的Text file enco ...

  8. Python_架构、同一台电脑上两个py文件通信、两台电脑如何通信、几十台电脑如何通信、更多电脑之间的通信、库、端口号

    1.架构 C/S架构(鼻祖) C:client  客户端 S:server  服务器 早期使用的一种架构,目前的各种app使用的就是这种架构,它的表现形式就是拥有专门的app. B/S架构(隶属于C/ ...

  9. 03-Linux的shell命令 .doc

    快捷键 基本操作和命令 Cd转换文件夹 以/开头的是绝对路径 没有/相对路径 ../代表上一级目录 Tab补充 Ctrl+R 查找历史输入过的命令 箭头上也代表能够查询以往输入的命令 Ctrl+C 终 ...

  10. js 判断字符串中是否包含某个字符串的方法实例

    String对象的方法 方法一: indexOf()   (推荐) var str = "123"; console.log(str.indexOf("3") ...