Press "Enter" to skip to content

BlogElul Day 12: Count

There may be nothing as quintessentially “tech”-y as counting. The first machine computers were counting machines, and before that it was an actual job title – as many learned when they watched the movie “Hidden Figures”.

But HOW we count – whether in human terms or those of a computer – says a lot about us. As human compute-ers (we all are, whether our relationship with numbers is fond or fraught), do we add on with our fingers (nothing shameful about it if you do)? Do we have answers at the ready from memorized tables? Do we use esoteric processes we’ve learned after hours of repetition?

On the hardware side things get more interesting (and for some of us who grew up slightly math-phobic, less overwhelming) the method we use to program a counting routine provides hours of amusement, if not philosophical intrigue.

A common entry-level question for a programming interview is to write out a program that mimics the childhood game of “fuzz buzz” ( Have your program print every number up to 100. For every number evenly divisible by 3, print “fizz”. For every number evenly divisible by 5, print “buzz”, and for numbers that can be evenly divisible by both 3 and 5, print “fizz-buzz”.

The simplest (but by no means most elegant answer) is to simply hard code each number:
print “1”;
print “2”;
print “fizz”;

…and so on.

Give an answer like that and the interview will know that you are either extremely salty or extremely inexperienced. Beyond that, the range of answer span the gamut of utility, functionality, clarity, and even elegance. It says (or at least CAN say) as much about a person as asking them my personal favorite interview question, “Tell me how to get to your favorite restaurant.”

But there’s another programming exercise related to counting that I find even more entrancing: the fibonacci sequence.

For those who missed it in school (or who didn’t get the joke in the book “Math Curse“), this is a sequence where each number is the product of the previous 2 numbers. So it starts off 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. For people with a modicum of coding experience, creating a program that executes a “fib sequence” a certain number of times is easy… until the professor (because this is one of their favorite first-year code school tricks) tells you you can only use a single variable.

One “slot” of data that has to hold the quantity of fib sequence numbers to show, plus the current number, plus the previous number. The answer lies in recursion, and the explanation is far beyond the scope of this blog.

But the result, regardless of the language you prefer to code in, has a simplicity, a clarity, and an elegance to it that is almost sublime once you see and understand it:

sub fibRec {
my $n = shift;
$n < 2 ? $n : fibRec($n - 1) + fibRec($n - 2);

A small program which calls itself, repeatedly, until it hits the bottom of the spiral, and then “winds” itself back out, generating numbers along the way.

My point in all of this – what this has to do with religion in general or the month of Elul in particular – is that our experiences can be defined by both how we approach something and how we view our relationship to that thing. Whether we view programming exercises like fizz-buzz and single-variable fib sequences as horrific chores or exciting challenges is about us, not the task. How we go about approaching and executing that task is says far less about our ability (because anyone can always learn a different technique or approach) and far more about our attitude.

In this month of Elul, where we not only count down the days until we stand before the Creator to be judged; but also spend each day taking account of our past actions and future intentions; in this time it is important to remember that, like any math exercise, it’s less about arriving at a single correct answer, and more about showing our work.

Leave a Reply

Your email address will not be published.

%d bloggers like this: