Exercise: Fibonacci

Run Settings
LanguagePython
Language Version
Run Command
# Given a number N return the index value of the Fibonacci sequence, where the sequence is: # 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ... # the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3 #For example: fibonacciRecursive(6) should return 8 def fibonacciIterative(n): if n == 0: return 0 elif n == 1 or n == 2: return 1 res = [0, 1, 1] for i in range(2, n): res.append(res[i] + res[i-1]) return res[-1] answer = fibonacciIterative(10) print(answer) def fibonacciRecursive(n): if n == 0: return 0 elif n == 1 or n == 2: return 1 else: return fibonacciRecursive(n-1) + fibonacciRecursive(n-2) answer = fibonacciRecursive(10) print(answer)
Editor Settings
Theme
Key bindings
Full width
Lines