[英](Java) Recursive Functions: using n-1 and n+1
I was recently given a project as follows: 我最近得到了一个如下项目:
Recursive relationship : x(n) = x(n+1) - 2x(n-1) where x(0) = 1 and x(2) = 3 Write a program where the user enters a number, n, and the nth value of x, as shown above is output. 递归关系:x(n)= x(n + 1) - 2x(n-1)其中x(0)= 1且x(2)= 3写一个程序,用户输入一个数字,n和n x的值,如上所示输出。
so far, I have made this method ( I am very new to, and really bad with [so far] recursion) 到目前为止,我已经制作了这个方法(我很新,并且[到目前为止]递归真的很糟糕)
public class x
{
static int x(int n)
{
if(n <= 1)
{
return 1;
}
if(n == 2)
{
return 3;
}
else
{
return x(n+1) - 2*(x(n-1));
}
}
and a simple main method that prompts the user for an input of n, and then prints the result of the above method: 和一个简单的主要方法,提示用户输入n,然后打印上述方法的结果:
public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scanner.nextInt();
System.out.println(x(n));
}
From the assignment, I calculated that x(1) = 1. [Being I am given x(0) = 1 and x(2) = 3]: 从赋值,我计算出x(1)= 1. [给我x(0)= 1和x(2)= 3]:
x(n) = x(n+1) - 2(x(n-1))
x(1) = x(2) - 2(x(0))
x(1) = 3 - 2(1)
x(1) = 3 - 2
x(1) = 1
which brought me to my base case shown in the static method x above, 这让我进入上面静态方法x中显示的基础案例,
if(n <= 1) // because 0 and 1 each return 1
{
return 1;
}
if(n == 2) // simply because 2 returns 3
{
return 3;
}
When I run what I have, the program gives me the error : 当我运行我所拥有的,程序给我错误:
Exception in thread "main" java.lang.StackOverflowError
at x.x(x.java:21)
Java Result: 1
Line 21 is the return statement in the else clause of the x method: 第21行是x方法的else子句中的return语句:
return x(n+1) - 2*(x(n-1));
and I have no idea what I can change to fix it. 我不知道我可以改变什么来解决它。 I have tried restating it as x(n+1)-x(n-1)-x(n-1), but it continues to give me the same error. 我已经尝试将其重新设置为x(n + 1)-x(n-1)-x(n-1),但它继续给我相同的错误。 I don't know if it is the base case being invalid and therefore not allowing the function to run it's course properly, or just the return statement I made is wrong. 我不知道它是否是无效的基本情况,因此不允许函数正确地运行它,或者只是我所做的return语句是错误的。 I also tried writing it out as if x(5) calls x(4) calls x(3), so on as my notes show, but I arrive at an issue regarding having n+1 and n-1 in the same function statement, so I think I am just going about it the wrong way. 我也尝试写出来,就像x(5)调用x(4)调用x(3)一样,正如我的笔记所示,但是我遇到了关于在同一个函数语句中有n + 1和n-1的问题所以我想我只是走错路。 Any guidance on this is extremely appreciated! 对此有任何指导非常感谢!
Issue is because it is never ending loop.... x(n) = x(n+1) - 2x(n-1) 问题是因为它永远不会结束循环.... x(n)= x(n + 1) - 2x(n-1)
You are incrementing n,ie, for 你正在递增n,即for
x(0) = 1 x(0)= 1
x(1) = x(2) - 2 x(0) x(1)= x(2) - 2 x(0)
x(3) = x(4) - 2 x(2).... x(3)= x(4) - 2 x(2)....
It keeps on going as there is no upper limit for x(n+1).... 它一直在继续,因为x(n + 1)没有上限....
Please check if you are not missing boundary condition for it from where recursion will terminate. 请检查您是否错过了递归将终止的边界条件。
Recurrence relation is defined as a function of the preceding terms. 递归关系定义为前述术语的函数。 So you have to redefine your recursive relationship as, 所以你必须重新定义你的递归关系,因为
x(n) = x(n+1) - 2x(n-1)
x(n+1) = x(n) + 2x(n-1)
x(n) = x(n - 1) + 2x(n-2)
Use above equation to implement the algorithm with base cases x(0) = x(1) = 1, x(2) = 3
. 使用上面的等式来实现具有基本情况x(0) = x(1) = 1, x(2) = 3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.