Skip to main content

Fibonacci

var data: Array<Number> = [1, 1]
func fib(num: Number) {    if (num < data.length) return data.get(num)
    var value = fib(num - 1) + fib(num - 2)    data.push(value)
    return value}
print(fib(100))

You might have noticed that fibonacci has already been done in a previous example. But in this example, we use arrays to store the values of the fibonacci sequence. The old method was incredibly slow because it had to calculate numbers that have already been calculated. In this method, it is stored in the array. This means that the function can get the nth fibonacci value very quickly. This example can get the 100th value in a split second.