Here's a recursive solution to find the nth Fibonacci number in Python:
main.py107 chars6 lines
And here's a dynamic programming solution that uses memoization to avoid duplicate calculations:
main.py195 chars9 lines
Both solutions have a time complexity of O(2^n) but the dynamic programming solution has a space complexity of O(n) because it stores the previously calculated values in memo. Therefore, the dynamic programming solution is more efficient and practical for finding large Fibonacci numbers.
gistlibby LogSnag