Software, Simplicity, Sustainability and Stuff

Hell Yeah, I Miss Being a Beginner

A new friend who contacted me via email for the first time a few weeks ago after reading a blog post of mine, recommended the book Hell Yeah or No by Derek Sivers1. The book is essentially a collection of posts on the subject of determining what's worth doing. I'm currently reading it, and several entries have reminded me of something I've lost.

I got my first computer in the early 1980s: an Acorn Atom with a mere 6K of RAM. The first few weeks, probably even months, I "wasted" just playing games on it. But after that, I got my feet wet with programming in BASIC, the language this computer was pre-programmed to understand.

The hardware at the time was very limited, and consequently, so were the things you could do with it. A challenge was to squeeze as much performance out of it as you could. One of the first programs I tried to write was one that calculated and listed prime numbers.

Refresher: a prime number is a whole number greater than 1 that can only be divided (without leaving a remainder) by 1 and itself.

The way you typically establish that a number is prime is by systematically trying to disprove it -- and failing to do so.

The first, extremely naive version of the program simply tried to divide a candidate prime by increasingly large numbers: first by 2, then 3, then 4, etc., until it had exhausted all numbers smaller than the candidate itself. If any of these divisions left no remainder, the candidate was not prime, and the program would move on to the next number.

I quickly realised that even numbers (other than 2) couldn't possibly be prime because they are, by definition, divisible by 2. From that moment on, I only considered odd numbers as candidates, thus speeding up the program. At the time, I foolishly assumed this made the program twice as fast because I'd halved the search space. Of course, this is not the case: yes, you did halve the search space but proving that an even number isn't prime is done as soon as you try to divide by 2.

The next step came with the realisation that the program didn't have to try dividing by numbers greater than half the candidate. For example, if you want to know whether 29 is prime, there's no point in testing 15 or anything larger: the result of the division will be less than 2, so you won't find any proper divisors that way.

I was never particularly good at maths, so when I started studying computer science at university years later, I was surprised when, during a first-semester calculus course, the lecturer mentioned in passing that "as you know, you only need to test divisors up to the square root of the number."2 I was surprised and embarrassed because I neither knew nor understood why this was the case. When I finally did understand, I was delighted by this new insight and the resulting speed-up3. It also made me realise that choosing a good algorithm can make far more difference to execution speed than micro-optimisations ever could.

At that point, the program would determine whether a candidate was prime by trying to divide it by all odd numbers (starting at 3) smaller than or equal to half the candidate itself. (As I mentioned, I only discovered the square root "trick" years later.)

Then another realisation struck me: the program didn't have to try dividing by 9, because it had already tried dividing by 3 and apparently failed. If a number isn't divisible by 3, it's definitely not going to be divisible by 9! This observation generalises: the program needs to divide the candidate only by numbers that were already known to be prime!

Without knowing it, I'd independently stumbled upon:

Dear reader, I suspect that this may make you shrug: "Big deal; it's nothing we didn't already know..." And I completely understand your indifference. But I still remember the feeling of exhilaration that accompanied those little discoveries in my mid teens.

They felt like a revelation after the immersion into the subject. I've experienced that feeling of discovery a few times since in varying degrees. It's what got me completely hooked on programming.

I must admit: as a corporate developer, it's been a long time since I last felt that. Most of my work nowadays falls into the "bin there, dun dat" category. I have to finish a certain number of story points worth of work items by the end of each sprint, making me feel like a factory worker on an assembly line. No time for wonder or exploration. I so long for the days when I discovered "new" truths!

--

  1. "Hell Yeah or No" - Derk Sivers, ISBN 9781988575971↩

  2. if a candidate is composite (i.e., not prime), it can be expressed as the product of two integers greater than 1. At least one of these factors must be smaller than or equal to the square root of that candidate; otherwise, their product would exceed the candidate itself. If no divisor has been found by the time you reach the square root, the candidate must be prime. E.g.: 49 = 7 x 7.↩

  3. the value of the square root of a candidate increases far, far more slowly than the half of a candidate. For example, if a candidate is 101, my version of the program would stop searching for divisors at 101 / 2 = almost 51. The version based on the square root would already stop at 11. The difference increases rapidly as the candidate becomes larger.↩