Recursive Fibonacci memoization

后端 未结 14 1403
走了就别回头了
走了就别回头了 2020-11-30 02:47

I need some help with a program I\'m writing for my Programming II class at universtiy. The question asks that one calculates the Fibonacci sequence using recursion. One mus

相关标签:
14条回答
  • 2020-11-30 03:06

    Here is my implementation of recursive fibonacci memoization. Using BigInteger and ArrayList allows to calculate 100th or even larger term. I tried 1000th terms, and result is returned in a matter of milliseconds, here is the code:

        private static List<BigInteger> dict = new ArrayList<BigInteger>();
        public static void printFebonachiRecursion (int num){
        if (num==1){
            printFebonachiRecursion(num-1);
            System.out.printf("Term %d: %d%n",num,1);
            dict.add(BigInteger.ONE);
        }
        else if (num==0){
            System.out.printf("Term %d: %d%n",num,0);
            dict.add(BigInteger.ZERO);
        }
        else {
        printFebonachiRecursion(num-1);
        dict.add(dict.get(num-2).add(dict.get(num-1)));
        System.out.printf("Term %d: %d%n",num,dict.get(num));
        }
    }
    

    Output example

    printFebonachiRecursion(100);
    
    Term 0: 0
    Term 1: 1
    Term 2: 1
    Term 3: 2
    ...
    Term 98: 135301852344706746049
    Term 99: 218922995834555169026
    Term 100: 354224848179261915075
    
    0 讨论(0)
  • 2020-11-30 03:08
    #include <stdio.h>
    long int A[100]={1,1};
    long int fib(int n){
        if (A[n])
        {
            return A[n];
        }
        else
        {
             return A[n]=fib(n-1)+fib(n-2);
        }
    }
    int main(){
        printf("%ld",fib(30));
    }
    
    0 讨论(0)
  • 2020-11-30 03:09
    public static int fib(int n, Map<Integer,Integer> map){
    
        if(n ==0){
            return 0;
        }
    
        if(n ==1){
            return 1;
        }
    
        if(map.containsKey(n)){
            return map.get(n);
        }
    
        Integer fibForN = fib(n-1,map) + fib(n-2,map);
        map.put(n, fibForN);
    
        return fibForN; 
    
    }
    

    Similar to most solutions above but using a Map instead.

    0 讨论(0)
  • 2020-11-30 03:10

    Might be too old but here is my solution for swift

    class Recursion {
        func fibonacci(_ input: Int) {
            var dictioner: [Int: Int] = [:]
            dictioner[0] = 0
            dictioner[1] = 1
           print(fibonacciCal(input, dictioner: &dictioner))
        }
    
        func fibonacciCal(_ input: Int, dictioner: inout [Int: Int]) -> Int {
            if let va = dictioner[input]{
                return va
            } else {
                let firstPart = fibonacciCal(input-1, dictioner: &dictioner)
    
                let secondPart = fibonacciCal(input-2, dictioner: &dictioner)
    
                if dictioner[input] == nil {
                    dictioner[input] = firstPart+secondPart
                }
    
                return firstPart+secondPart
            }
        }
    }
    
    // 0,1,1,2,3,5,8
    class TestRecursion {
        func testRecursion () {
            let t = Recursion()
            t.fibonacci(3)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:11

    You need to distinguish between already calculated number and not calculated numbers in the dictionary, which you currently don't: you always recalculate the numbers.

    if (n == 0) 
    {
      // special case because fib(0) is 0
      return dictionary[0];
    }
    else 
    {
      int f = dictionary[n];
      if (f == 0) {
        // number wasn't calculated yet.
        f = fibonacci(n-1) + fibonacci(n-2);
        dictionary[n] = f;
      }
      return f;
    }
    
    0 讨论(0)
  • 2020-11-30 03:11

    This is another way to approach memoization for recursive fibonacci() method using a static array of values -

    public static long fibArray[]=new long[50];\\Keep it as large as you need
    
    public static long fibonacci(long n){
    long fibValue=0;
    if(n==0 ){
        return 0;
    }else if(n==1){
        return 1;
    }else if(fibArray[(int)n]!=0){
        return fibArray[(int)n];    
    }
    else{
        fibValue=fibonacci(n-1)+fibonacci(n-2);
        fibArray[(int) n]=fibValue;
        return fibValue;
    }
    }
    

    Note that this method uses a global(class level) static array fibArray[]. To have a look at the whole code with explanation you can also see the following - http://www.javabrahman.com/gen-java-programs/recursive-fibonacci-in-java-with-memoization/

    0 讨论(0)
提交回复
热议问题