function fibonacci_iter(n) k = ones(Int,3) indexer(i) = mod1(i,3) for i in 3:n k[indexer(i)] = k[indexer(i-1)] + k[indexer(i-2)] end return k[indexer(n)] end function fibonacci_recursive(n) n <= 0 && return 0 n == 1 && return 1 return fibonacci_recursive(n-1) + fibonacci_recursive(n-2) end println("Benchmark for iterative implementation:") @btime fibonacci_iter(42) println("Benchmark for recursive implementation:") @btime fibonacci_recursive(42) #Trying higher values tends to take too long. """ Since the fibonacci recursion has just three terms we can get any element of the sequence by using an array of three elements. On the contrary if one wants the 123rd element of the array and naively needs initializes an array of 123 elements one ends up using too much memory for something so simple, and where the recursive had a bottleneck of operations this naive way has a bottleneck of memory. """