用Python求一个数的平方根.
来源:学生作业帮 编辑:神马作文网作业帮 分类:英语作业 时间:2024/10/12 01:33:15
用Python求一个数的平方根.
At each iteration (loop) of the algorithm,the approximation x is replaced by the average of x and s divided by x.Written as an assignment statement,it looks like this:
x = (x + s/x)/2.0.There are various criteria for deciding when to stop improving the answer of a square root algorithm.In your implementation,I want you keep looping until the square of x is very close to the value of s.In other words,you want to reduce the error of your approximation x until it is acceptably small.You can measure the relative error by comparing the absolute difference of x squared and s.This is written mathematically as:
error = | x * x - s |.Use the math.fabs() function to compute absolute value.
So keep looping and calculating better and better values for x until the error is less than some small constant.For
your implementation of heron(s),stop when error < 0.0000001 which is a small discrepancy.Add a print
statement inside your algorithm loop which prints out both x and x*x at each step so you can see how fast it converges to a good answer.It is very interesting to watch it work.Try different starting guess values for x and see if it
makes any difference to the number of loops required (always use a positive guess value)
At each iteration (loop) of the algorithm,the approximation x is replaced by the average of x and s divided by x.Written as an assignment statement,it looks like this:
x = (x + s/x)/2.0.There are various criteria for deciding when to stop improving the answer of a square root algorithm.In your implementation,I want you keep looping until the square of x is very close to the value of s.In other words,you want to reduce the error of your approximation x until it is acceptably small.You can measure the relative error by comparing the absolute difference of x squared and s.This is written mathematically as:
error = | x * x - s |.Use the math.fabs() function to compute absolute value.
So keep looping and calculating better and better values for x until the error is less than some small constant.For
your implementation of heron(s),stop when error < 0.0000001 which is a small discrepancy.Add a print
statement inside your algorithm loop which prints out both x and x*x at each step so you can see how fast it converges to a good answer.It is very interesting to watch it work.Try different starting guess values for x and see if it
makes any difference to the number of loops required (always use a positive guess value)
下面代码定义一个函数heron(s)用迭代的方法取得平方根,其中x=s/2可以使用x=s/3,s/5之类的多个值实验一下,看分别需要多少步.
以s=500为例,
x=s时需要9步
x=s/2时需要8步
x=s/3时需要7步
x=s/5时需要5步
'''
Created on 2011-10-26
@author:legendxx
'''
import math
def heron(s):
x=s/2
count=0
sqr=x*x
while math.fabs(sqr - s)>=0.0000001:
count+=1
x = (x + s/x)/2.0
sqr=x*x
print count,":",x,sqr
print count,"steps needed"
if __name__ == '__main__':
s=float(raw_input("input a number"))
heron(s)
以s=500为例,
x=s时需要9步
x=s/2时需要8步
x=s/3时需要7步
x=s/5时需要5步
'''
Created on 2011-10-26
@author:legendxx
'''
import math
def heron(s):
x=s/2
count=0
sqr=x*x
while math.fabs(sqr - s)>=0.0000001:
count+=1
x = (x + s/x)/2.0
sqr=x*x
print count,":",x,sqr
print count,"steps needed"
if __name__ == '__main__':
s=float(raw_input("input a number"))
heron(s)
数x的一个平方根为3,求数x的另一个平方根
求一个数A的平方根的运算,叫做什么
求一个数的平方根的运算叫做
求一个数的平方根过程?举个例子9
已知-2是某数的一个平方根,求这个数和它的算术平方根.
一个数的算术平方根为a,求比这个数大16的数是多少?
python 判断每行第一个数和最后一个数的尾数是否相等,输出结果
已知某数的一个平方根为根号13,求这个数和它的另一个平方根
若一个数a的平方根等于它本身,数b的算术平方根也等于它本身,试求a+b的平方根.
一个数的正的平方根是算术平方根.
一个数A的算术平方根为2M-6,它的平方根为正负(M-2),求这个数A及其平方根
求解释python中这几句话的意思