Sort by

recency

|

300 Discussions

|

  • + 0 comments

    def fibonacci(n): a, b = 1, 1 for _ in range(n - 1): a, b = b, a + b return a

  • + 0 comments
    ans = {0,1}
    def fibonacci(n):
        if n <= 1:
            return 1
        c =fibonacci(n-1) + fibonacci(n-2)
        ans.add(c)
        return c
    
    n = int(input())
    fibonacci(n)
    ans = sorted([i for i in ans])
    print(ans[n-1])
    
  • + 0 comments

    It's weird to me that the Fibonacci sequence is academically used as the "hello world" of recursion because I mean:

    def fibonacci(n):
        if n == 1:
            return(1)
        upper = 1
        lower = 1
        for i in range(2,n):
            upper, lower = lower + upper, upper
        return(upper)
    
  • + 0 comments

    Python Dynamic programming

    def fibonacci(n, memo=None):
        if memo is None:
            memo = [None] * (n + 1)
        if n <= 1:
            return n
        if memo[n] is None:
            memo[n] = fibonacci(n -2, memo) + fibonacci(n-1, memo)
        return memo[n]
    
  • + 0 comments

    java 8 solution

     public static int fibonacci(int n) {
    
           if (n <= 1) {
                return n;
            }
            return fibonacci(n - 1) + fibonacci(n - 2);
        
            // Complete the function.
        }