python中没有预定义函数的平方根 [英] square root without pre-defined function in python

查看:33
本文介绍了python中没有预定义函数的平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用python中任何预定义函数的情况下找到数字的平方根?

How can one find the square root of a number without using any pre-defined functions in python?

我需要程序的平方根如何工作的主要逻辑.在一般数学中,我们将使用 HCF 来完成,但在编程中,我找不到逻辑.

I need the main logic of how a square root of a program works. In general math we will do it using HCF but in programing, I am not able to find the logic.

推荐答案

有一种著名的数学方法,称为 Newton-Raphson 方法,用于逐次找到根的更好近似值.

There is a famous mathematical method called Newton–Raphson method for finding successively better approximations to the roots.

基本上,此方法采用初始值,然后在成功的迭代中收敛到解决方案.您可以阅读有关它的更多信息 这里.

Basically , this method takes initial value and then in successful iterations converges to the solution.You can read more about it here.

此处附上示例代码供您参考.

Sample code is attached here for your reference.

def squareRoot(n):
    x=n
    y=1.000000 #iteration initialisation.
    e=0.000001 #accuracy after decimal place.
    while x-y > e:
        x=(x+y)/2
        y=n/x
    print x

n = input('enter the number : ') 
squareRoot(n)

在这里您可以通过在小数点后的 e 和 y 中添加0"数字来提高平方根结果的准确性.

Here you can increase the accuracy of square root result by adding '0' digits in e and y after decimal place.

还有其他方法,如二分搜索,用于寻找如图所示的平方根 这里.

Also there are other methods like binary search for finding square roots like shown here.

这篇关于python中没有预定义函数的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆