Chapter 1. Small problems
To get started, we will explore some simple problems that can be solved with no more than a few relatively short functions. Although these problems are small, they will still allow us to explore some interesting problem-solving techniques. Think of them as a good warmup.
The Fibonacci sequence is a series of numbers such that any number, except for the first and second, is the sum of the previous two:
The value of the first Fibonacci number in the series is 0. The value of the fourth Fibonacci number is 2. It follows that to get the value of any Fibonacci number, n, in the series, one can use the formula
The preceding formula for computing a number in the Fibonacci sequence (illustrated in figure 1.1), a form of pseudocode, can be trivially translated into a recursive Swift function (a recursive function is a function that calls itself). This mechanical translation will serve as the first version of our attempt at writing a function to return a given value of the Fibonacci sequence:
Figure 1.1. The height of each stickman is the addition of the previous two stickmen’s heights added together.
