A Fibonacci Server

Learn about the Fibonacci series and its processing in Elixir.

Introduction

Let’s round out this chapter with an example program. Given a list of nn, our task is to calculate fib(n)fib(n), where fib(n)fib(n) is the nthn^{th} Fibonacci number. The Fibonacci sequence starts from 0, 1. Each subsequent number is the sum of the preceding two numbers in the sequence. We chose this not because it’s something we all do every day but because the naive calculation of Fibonacci numbers from 10 to 37 takes a measurable number of seconds on typical computers.

Implementation

The twist is that we’ll write our program to calculate different Fibonacci numbers in parallel. To do this, we’ll write a trivial server process that does the calculation and a scheduler that assigns work to a calculation process when it becomes free. The following diagram shows the message flow.

Get hands-on with 1200+ tech skills courses.