I’m writing this in gorgeous Cape Town, where I’ve had plenty of occasion, oddly enough, to think of numbers and mathematics. Consider, for example, the kilometres we’ve racked up on our rental car: some 2,000 and counting, with plenty more to come. I’m doing the arithmetic in my head: the car travels an abysmal 10km to the litre, petrol is about 10 rand per litre, so we’re spending about a rand a km, which works out to…some painfully large sum already spent on petrol alone, which helps explain the velocity at which money vanishes from our pockets in this country…so to console myself, I’ve been turning to other mathematical reminders.

Example: cormorants. You know, those black birds with long necks that bob about in the water and sometimes rouse themselves to fly somewhere, flapping steadily and frenetically just inches above the waves? Yeah, those guys. Our boat to Robben Island sliced through flocks of alarmed cormorants, driving them to take wing, skeins at a time.

Skeins? you ask. What’s he mean by that, skeins? Well, if you’ve ever watched these black beauties in flight, you’ll have noted that they fly in a straggly “V", usually with unequal arms, but a V nevertheless. Just what we saw on the water as we chugged toward Robben Island. But why so? More broadly, how and why do flocks of birds maintain a certain formation while flying? Why do these flocks appear to demonstrate a collective instinct, even an intelligence? What elaborate rules govern this motion?

In the 1980s, a bird-flock-watcher called Craig Reynolds tried to answer just that. But he believed that, far from complex rules for the flock as a whole, there were three simple rules that each individual bird followed. Luckily for the rest of us, Reynolds was also a computer scientist. So he wrote a program to apply his rules to simulated flocks of birds, calling it “Boids".

Three rules, but what Reynolds produced was stunning. The imaginary creatures “flew" across his screen uncannily like real birds would. Individuals placed randomly would swiftly gather and head in the same direction. The centre of the flock stayed relatively stable; boids at the edges adjusted their speed to keep up with the rest. Place an obstacle in their path and the gang of boids would smoothly divideforces, fly around it and join up again at the far side.

Uncannily like the cormorants in Cape Town.

So what were the rules Reynolds came up with? They were:

1. Cohesion: Move towards the average position of my flockmates.

2. Alignment: Maintain the same speed and direction as my flockmates.

3. Separation: Maintain a certain distance from my flockmates, and from other objects, so I won’t bump into them.

Stunning. Obeying three fairly intuitive rules, Reynolds’ boids were doing a fine job imitating a flock of real birds. This is not to say that real birds follow these rules. We don’t know, and yet for flock-watchers, it doesn’t really matter. What is truly amazing about Reynolds’ experiment is that nowhere in his lines of software had he encoded rules for the flock itself. Which gives us this profound insight: from simple individual rules, we get collective behaviour.

All right, so we have an idea of how birds fly. So what? Well, the boids experiment was extended to simulate the behaviour of fish, of ants. All right, you’re asking again, but so what? Well, similar ideas were also used to simulate crowds as they surged towards the gates of stadiums; this allowed organizers to work out where best to place ticket collectors. And Reynolds’ work found some Hollywood vindication as well: it was used in the stampede scene in TheLion King.

But there are deeper lessons too. Boids was an example of what’s called “bottom-up" programming. Instead of any number of instructions for what a computer must do in every given situation—the “top-down" approach—you lay down a few broad brush strokes which work together to build the more complex system you need. Of course it’s hard work to find the right strokes. But if you can do it, your software will likely be simpler, clearer and more elegant.

The idea of complexity growing out of simplicity is a powerful, far-reaching one. It now goes by the term “emergence". This refers to how a collective system’s behaviour depends not on its individual components, but on how they interact. You will find emergence everywhere: from crowds to ants, birds to economies. So now you know how this stuff has been playing out in my mind. Using some basic rules—you buy something, you pay for it; you eat something, you pay for it; you drive something, you pay for it—I’m personally responsible for a substantial fraction of the undoubtedly intricate South African economy. One kilometre at a time, one rand at a time, too many damned rands per day. Think I’ll go back to watching cormorants.

Once a computer scientist, Dilip D’Souza now lives in Mumbai and writes for his dinners. A Matter of Numbers will explore the joy of mathematics, with occasional forays into other sciences. Comments are welcome at dilip@livemint.com