且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我需要一个简单而有效的算法来找出一个数字的平方根(***是一个4位数字)

更新时间:2023-02-10 14:54:04

我知道有一种方法可以手动计算平方根,但是那个方法并不是特别有用在计算机上计算它。计算机可以将数字乘以和除以多位数,执行时间几乎相同,无论因素是单个数字还是包含许多小数位。



之一计算机最简单的平方根算法就是所谓的巴比伦方法(实际上是牛顿算法用于求解方程式的应用):



 x =  0  5  *(x + s / x); 



s是您想要取平方根的数字。请参见 http://en.wikipedia.org/wiki/Square_root_algorithm [ ^ ]。



对于起始值,您可以选择x = s / 4。上述公式的四次迭代应该给出大约6位有效数字的精度。


这种算法已在CPU中实现。也就是说,看起来你的问题不是一个真正的问题,而是一个出于教育目的的问题。这意味着,很可能,这是某种家庭作业,而且这是一个非常有趣的。



如果有人做你的家庭作业,它会帮助你吗为了你?我不这么认为。 你不仅需要另一个学分,而且你真的需要接受教育。如果你不自己完成任务,你就会失去另一个获得更多知识和经验的机会。失去这个机会对你不利。所以,利用你学校给你的机会。



-SA


做类似的事情这个:

  double  square_root( double  low, double  high)
{
double mid,result = 0 0 ;

mid =(高+低)/ 2 ;

// 基本案例
if ((mid * mid)== high){
result = mid;
} else if ((high - low)< 0 001 ){
// 当高和低数字在0.001之内时停止。
result = mid;
} else if ((mid * mid)> 高){
result = square_root(low,mid);
} 其他 {
result = square_root(mid,high);
}
返回结果;
}



或者,

见..

http://edisontus.blogspot.com/2013/04/square-root-algorithm-for-c.html [ ^


Hello everyone,
i need a simple algorithm for finding out square root a 4 digit number...
I know how to find out it on PAPER but designing algorithm is not being successful...plz help me out..

I know there is a method for calculating the square root manually, but that one ins not particularly useful for calculating it on a computer. Computers can multiply and divide numbers with many digits and the execution time is nearly the same, no matter the factor are a single digit or contain many decimal places.

One of the easiest square root algorithms for a computer is the so-called Babylonian method (in fact an application of the Newton algorithm for solving an equation):

x = 0.5 * (x + s/x);


s being the number that you want to take the square root of. See http://en.wikipedia.org/wiki/Square_root_algorithm[^].

For the starting value you might choose x=s/4. Four iterations of the above formula should give you a precision of about 6 significant digits.


Such algorithm is already implemented in the CPUs. That said, it looks like your problem is not a real problem, but the one posed for educational purpose. It means that, most likely, this is some kind of home assignment, and this one is a pretty interesting one.

Will it help you if someone does your home assignment for you? I don't think so. Not only you need yet another credit, but you really need education. By not doing your assignment by yourself, you are loosing yest another chance to get some more knowledge and experience. And loosing this chance is not good for your. So, use your chances which your school gives you.

—SA


Do something like this:
double square_root(double low, double high)
{
    double mid, result = 0.0;

    mid = (high + low) / 2;

    //Base Case
    if((mid*mid) == high) {
        result = mid;
    } else if((high - low) < 0.001) {
        //Stops when the high and low numbers are within 0.001 of one another.
        result = mid;
    } else if((mid*mid) > high) {
        result = square_root(low, mid);
    } else {
        result = square_root(mid, high);
    }
    return result;
}


Or,
See..
http://edisontus.blogspot.com/2013/04/square-root-algorithm-for-c.html[^]