Skip to content

Commit

Permalink
added a iterative version
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bender committed May 4, 2018
1 parent c36a410 commit 31c9781
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dp/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ def fib_list(n):
return list_results[n]

# print(fib_list(100)) # => 354224848179261915075

def fib_iter(n):
"""[summary]
Works iterative approximate O(n)
Arguments:
n {[type]} -- [description]
Returns:
[type] -- [description]
"""

fib_1 = 0
fib_2 = 1
sum = 0
if n <= 1:
return n
for i in range(n-1):
sum = fib_1 + fib_2
fib_1 = fib_2
fib_2 = sum
return sum

# => 354224848179261915075
# print(fib_iter(100))

0 comments on commit 31c9781

Please sign in to comment.