Fibonacci series in depth, using Swift

An iterative approach

f(0) = 0
f(1) = 1
var a = 0
var b = 1

A recursive approach

The recursion tree
return fib(n - 1) + fib(n - 2)
f(4) = f(3) + f(2) = f(2) + f(1) = f(1) + f(0) + f(1)
The external cache defined here can be accessed by any instance of fib
Passing the cache requires the use of inout parameters

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store