Widget Wednesdays #19

This week I am going to something different for Widget Wednesdays. I am going to describe my favorite “simplest computer program”.

When somebody tells me that they could never learn how to program, this is the program that I scribble down on a napkin for them. Some of you might be familiar with this — it’s quite beautiful.

Most of us learned about the Fibonacci sequence when we were kids. It describes so many things in nature, from the shape of flower petals to how populations grow.

The principle is very simple. Starting with the numbers 0 and 1, you just keep adding the last two numbers to get the next one:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Nicely enough, a program to compute the nth number in the series looks like this:

fibonacci(n) is
    if n < 2
        then n
    else
        fibonacci(n-2) + fibonacci(n-1)

No matter how math-phobic you think you are, that is a very easy computer program to understand. And it contains many of the core ideas in computer programming — functions, variables, conditionals, arithmetic, and even recursion.

Which makes me hopeful for the whole process of teaching programming. As an ancient Chinese proverb never said: “A program of a thousand lines begins with a single step.”

Leave a Reply