From 2a6711ab6b09f5ab7bb066cd6c11670e6ff2bd90 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Mon, 13 Aug 2018 22:20:34 -0500 Subject: Week2 Python Fibonacci done! --- AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 AlgoDesignAndTechniqueEdxPython/sources/fibonacci.py 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)) + -- cgit v1.2.3