(Java)递归算法的问题 - 平方根 [英] (Java) problem with recursion algorithm - square roots

查看:266
本文介绍了(Java)递归算法的问题 - 平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序应该取(最终)数字x并计算它的平方根。它将在n次迭代后停止。此外,它将通过递归解决。





我的代码到目前为止:



The programm should take the (final) number x and calculate the square root of it. It shall stop after n iterations. Furthermore it shall be solved with recursion.


My code so far:

public class Babylon{

    double sqrt( final double x, final int n ) {

        double s;

        if ( n == 0 ) {
            return x;
        }



        else {
            s=sqrt(0.5*(x+x/x),n-1); // I guess that the error must be here
            return 0.5*(s+x/s);
        }
    }       

public static void main (String[] args) {

    Babylon t = new Babylon();

    t.sqrt(10,8); // That is for testing reasons.


}
}   





我的尝试:



我尝试用s = sqrt(0.5 *(s + x / x),n-1);和公式的所有变化。还试图在返回语句之后放置sqrt。



What I have tried:

I tried it with "s=sqrt(0.5*(s+x/x),n-1)"; and all variations of the formula. Also tried to put the "sqrt" right after the return statement.

推荐答案

你需要做这样的事情:

You need to do something like this:
double sqrt(double S, double x, double n)
{

    if (n == 0)
        return x;
    else
        return sqrt(S,0.5*(x+S/x), n - 1);

}

S 永远不会改变,因为它是你想要的平方根,而 x 将会改变。并且你必须猜测一个初始的x值,它不能为零,让我们说0.1,所以调用可以是这样的:

S will never change since its the number you want the square root of, while x will change. And you have to guess an initial x value to start with which cannot be zero, let us say 0.1, so the call can be like this:

x_n = sqrt(S, 0.1, 10);

如果你考虑一下,你试图在 x ^ 2 = S 时找到,我们知道<$ c时$ c> x = sqrt(S) S / x = x ,依次表示 0.5 *( x + S / x)= x

If you think about it you are trying to find when x^2 = S, and we know that when x = sqrt(S) that S/x = x, whitch in turn means that 0.5*(x+S/x) = x.


我不知道你尝试做什么,但是:

x / x 为1,(x + x / x) x + 1

猜猜你想要使用巴比伦方法,你需要跟踪你搜索的平方根及其当前估计值。

计算平方根的方法 - 维基百科 [ ^ ]



有一个工具可以让你看到你的代码是什么正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
I don't know what you try to do, but:
x/x is 1, and (x+x/x) is x+1
Guessing you want to use the Babylonian method, you need to keep track of the square root you search and its current estimate.
Methods of computing square roots - Wikipedia[^]

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于(Java)递归算法的问题 - 平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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