我需要一个简单而有效的算法来找出一个数字的平方根(最好是一个4位数字) [英] I need a simple and working algorithm for finding out the squareroot of a number (preferably a 4 digit number)

查看:40
本文介绍了我需要一个简单而有效的算法来找出一个数字的平方根(最好是一个4位数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i需要一个简单的算法来找出4位数的平方根...

我知道如何在PAPER上找到它但是设计算法没有成功... plz帮帮我..

解决方案

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



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



 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[^]


这篇关于我需要一个简单而有效的算法来找出一个数字的平方根(最好是一个4位数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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