summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2018-08-13 22:20:34 -0500
committerHaidong Ji2018-08-13 22:20:34 -0500
commit2a6711ab6b09f5ab7bb066cd6c11670e6ff2bd90 (patch)
tree1f8f9487ff77171431e445c374677632818b8c7e
parent15ff94736221bfdf8c3b60457409b9c4d350aa4b (diff)
Week2 Python Fibonacci done!
-rw-r--r--AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py b/AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py
new file mode 100644
index 0000000..debfb9e
--- /dev/null
+++ b/AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py
@@ -0,0 +1,20 @@
+# Uses python3
+def calc_naive(n):
+ if (n <= 1):
+ return n
+
+ return calc_naive(n - 1) + calc_naive(n - 2)
+
+def calc_optimize1(n):
+ if (n <= 1):
+ return n
+ fibLst = [None] * (n+1)
+ fibLst[0] = 0
+ fibLst[1] = 1
+ for j in range(2, n+1):
+ fibLst[j] = fibLst[j-1] + fibLst[j-2]
+ return fibLst[n]
+
+n = int(input())
+print(calc_optimize1(n))
+