如何使用斐波那契方法实现尾递归? [英] How do I implement Tail recursion with my Fibonacci method?

查看:59
本文介绍了如何使用斐波那契方法实现尾递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算大量的斐波那契数列,因此为什么要使用大整数.我可以达到大约10000,但是我用光了堆栈空间.我意识到我可以增加堆栈和堆空间,但是据我了解,尾递归可以解决空间问题.这是我的代码.

I'm trying to compute large numbers of the Fibonacci sequence, hence why I am using big integer. I can get up to about 10000 the way it is, but I run out of stack space. I realize I can increase stack and heap space, but it is my understanding that tail recursion can get around the space issue. Here is my code..

public class FibRecursion{

static BigInteger[] fval;

public static void main(String[] args) {

    int index;

    Scanner input = new Scanner(System.in);

    index = input.nextInt();


    fval = new BigInteger[index + 1];


    System.out.println(fib_rec(index));


}


public static BigInteger fib_rec(int index){


    BigInteger result = BigInteger.ONE;

    if(index <= 2){
        return result;
    }

    else{
        if(fval[index] != null){
            result=fval[index];
        }
        else{
            result = fib_rec(index-1).add(fib_rec(index-2));

            fval[index] = result;

        }
        return result;
    }
}  
}

推荐答案

实现您想要的系列的简单递归可以是:

A simple recursion to achieve the series you want could be :

public class FibRecursion{

    private static BigInteger[] fval;

    public static void main(String[] args) {

        int index = 10;
        fval = new BigInteger[index];
        fib(0,1,0,index);
        System.out.println(Arrays.toString(fval));
    }

    public static void fib(long a, long b, int index, int endIndex ) {

        if (index >= endIndex) {

            return ;
        }

        fval[index] = BigInteger.valueOf(a).add(BigInteger.valueOf(b));
        index++;
        fib(b, a+b, index , endIndex);
    }
}

为避免堆栈限制,您可以限制递归深度并以几个片断"形式进行复活.这是一系列50个元素的示例,其深度限制为10( RECURRSION_DEPTH = 10 ):

To avoid stack limitations, you can limit the recursion depth and do the resurrection in a few "pieces". Here is an example of a series of 50 elements, calculated with depth limited to 10 (RECURRSION_DEPTH = 10):

public class FibRecursion{

    private static BigInteger[] fval;
    //limit of the recursion depth. valid values are >=2
    private final static int RECURRSION_DEPTH = 10;

    public static void main(String[] args) {

        int index = 50;
        fval = new BigInteger[index];

        BigInteger aValue = BigInteger.valueOf(0);
        BigInteger bValue = BigInteger.valueOf(1);
        int startIndex = 0;
        int endIndex = RECURRSION_DEPTH;

        while (endIndex > startIndex) {

            fib(aValue,bValue,startIndex,endIndex);

            aValue = fval[endIndex-2];
            bValue = fval[endIndex-1];
            startIndex = endIndex;
            endIndex = Math.min(endIndex + RECURRSION_DEPTH, index);
        }

        System.out.println(Arrays.toString(fval));
    }

    //use BigInteger to avoid integer max value limitation 
    public static void fib(BigInteger a, BigInteger b, int index, int endIndex ) {

        if (index >= endIndex) {

            return ;
        }

        fval[index] = a.add(b);
        index++;
        fib(b, a.add(b), index , endIndex);
    }
}

这当然还有其他限制,与堆栈大小无关.

This of course has other limitations, not related to stack size.

这篇关于如何使用斐波那契方法实现尾递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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