Fibonacci and Dynamic Programming

Table of Contents

  1. Fibonacci
  2. Recursion
  3. Memoize
  4. Bottom-up

Fibonacci

1, 1, 2, 3, 5, 8, …

fib(n)
fib(3) = 2
fib(5) = 5
fib(6) = 8
...

Recursion

# Recursion

def fib(n):
    if n == 1 or n ==2:
        result = 1
    else:
        result = fib(n-1) + fib(n-2)
    return result

Pros: simple to write and simple to understand
Cons: too slow, due to high computation cost

Store (Memoize)

# Memoized Solution

def fib_2(n, memo):
  if memo[n] is not None:
    return memo[n]
  if n == 1 or n == 2: 
    result = 1
  else:
    result = fib_2(n-1, memo) + fib_2(n-2, memo)
  memo[n] = result
  return result

def fib_memo(n):
  memo = [None] * (n + 1)
  return fib_2(n, memo)

Pros: runs fast
Cons: recursion depth limitation:

fib_memo(1000)

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-9-46b166948401> in <module>()
----> 1 fib_memo(1000)

2 frames
... last 1 frames repeated, from the frame below ...

<ipython-input-5-4df1e59f88e4> in fib_2(n, memo)
      6     result = 1
      7   else:
----> 8     result = fib_2(n-1, memo) + fib_2(n-2, memo)
      9   memo[n] = result
     10   return result

RecursionError: maximum recursion depth exceeded in comparison

Bottom-up

# Bottom-up

def fib_bottom_up(n):
  if n == 1 or n ==2:
    return 1
  bottom_up = [None] * (n+1)
  bottom_up[1] = 1
  bottom_up[2] = 1
  for i in range(3, n+1):
    bottom_up[i] = bottom_up[i-1] + bottom_up[i-2]
  return bottom_up[n]

Pros: fast, and easy to understand
Cons: none

fib_bottom_up(1000)

43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值