简体   繁体   English

Java Fibonacci递归代码

[英]Java Fibonacci recursion code

I am trying to come up with a program to take from a user any number and produce the nth number for the fibonacci code. 我试图提出一个程序,以从用户获取任何数字,并为斐波那契代码生成第n个数字。 When I complete my work it's showing the next # instead of the # I need. 当我完成工作时,它会显示下一个#而不是我需要的#。 For instance I'm looking for the 11th # and its producing 233 instead of 144. Here is my code: 例如,我正在寻找第11个#及其产生的233,而不是144。这是我的代码:

public static int fibonacci(int n)
{
    if (n<=0)
        return 1;
    else
        return fibonacci(n-2)+ fibonacci(n-1);
}


public static void main(String[] args)
{
    System.out.println("Enter a Number:");
    Scanner keyboard = new Scanner(System.in);
    int number = keyboard.nextInt();
    System.out.println("You Entered Number:" + number);
    System.out.println(number + "th Fibonacci Number is:"+ fibonacci(number));
    keyboard.close();        
}

It's supposed to be 应该是

if(n == 0)
    return 0;
else if(n == 1)
    return 1;
else
    return fibonacci(n - 1) + fibonacci(n - 2);

because fibonacci(0) = 0 因为fibonacci(0)= 0

You are missing an index. 您缺少索引。 You are printing the next Fibonacci number. 您正在打印下一个斐波那契数。 Change the 改变

(n<=0)

to

(n<=1)

EDIT: 编辑:

as the other answer says, fib(0) = 0, so you have to add this edge case too. 正如另一个答案所说,fib(0)= 0,因此您也必须添加这种边缘情况。

Aleksandar already gave you the answer, still I think it is necessary to warn you that your code is extremely inefficient. Aleksandar已经为您提供了答案,但我仍然认为有必要警告您,您的代码效率极低。 Indeed, for computing fibo(4) , you are going to compute fibo(3) and fibo(2) . 实际上,对于计算fibo(4) ,您将要计算fibo(3)fibo(2) fibo(3) will require to compute fibo(2) and fibo(1) etc. Finally, you will take a lot of time computing already computed results (and you will use an exponential amount of memory for storing the local recursive calls context). fibo(3)将需要计算fibo(2)fibo(1)等。最后,您将花费大量时间来计算已计算的结果(并且将使用指数级的内存来存储本地递归调用上下文) 。

For a more efficient code, you ned to implement an iterative or a tail-recursive method. 为了获得更有效的代码,您必须实现迭代或尾递归方法。 A tail-recursive method meth is a method which recursive calls are of the form meth(f(args)) . 尾递归方法meth是一种递归调用形式为meth(f(args)) In your case, your recursive call has the form f(meth,args) so it is not tail-recursive. 在您的情况下,递归调用的形式为f(meth,args)因此它不是尾递归的。

Here is a tail-recursive version : 这是尾递归的版本:

public static int fibonacci(int n) {
    if (n == 0)
        return 0;
    else if (n <= 2)
        return 1;
    else 
        return fibonacciAux(0,1,n);
}

public static int fibonacciAux(int a, int b, int n) {
    if (count == 0)
        return b;
    else  
        return fibonacciAux(b,a + b,n - 1);
}

This version will run in linear time and will use a constant amount of memory, compared to a non-tail recursive version which will use an exponential time and memory. 与使用指数时间和内存的非尾递归版本相比,该版本将以线性时间运行并使用恒定数量的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM