At a lecture I attended recently, the speaker shared some mysterious instructions:

When [an even number is] halved, two [is written];
when [an odd number is diminished by] 1, [write] 0;
where 0 [is written], doubled;
where halved, multiplied by itself.

It is not at all clear to someone with my mathematical and cultural background, but the lines above are a millennia-old algorithm to compute powers of 2. They appear in Chandaḥśāstra, a treatise on Sanskrit prosody by Indian mathematician Piṅgala, who lived around the 2nd or 3rd century BCE.

Even after being told that the above lines contain instructions for computing powers of 2, I was not sure how to deploy them. Luckily, Kim Plofker, who was giving the lecture, demonstrated with an example. To find 24, the algorithm produces this sequence of computations.

4 4/2=2 2/2=1 1−1=0
2 2 0
16 4 2 1

On the top line, working from left to right, we start with the given number, 4, and then divide by 2 if the number is even or subtract 1 if the number is odd, iterating the process until we get 0. On the middle line, we record a 2 if we halve and 0 if we subtract. On the bottom line, working from right to left, we start with the number 1 and then double if we see a 0 in the line above or square if we see a 2.

Here’s the process with a larger number, 7. To compute 27:

7 7−1=6 6/2=3 3−1=2 2/2=1 1−1=0
0 2 0 2 0
128 64 8 4 2 1

I played with the algorithm a few times and started to understand why it always produced the correct answer. Squaring a number doubles the exponent, and for the base 2, adding one to the exponent corresponds to doubling the number. This method uses fewer steps than simple repeated doubling does, and I found it oddly satisfying to play with.

Piṅgala recorded this algorithm in service of literature. In Sanskrit, a syllable can be either heavy or light, and he was determining the number of possible combinations of heavy and light syllables that could exist in a meter with n syllables. (Not all of these meters were actually used in literature, so the question was one of theory, not literary practice.) His text includes other computations related to counting meters with certain numbers of heavy or light syllables.

Once interpreted, Piṅgala’s algorithm was satisfying to me, but his instructions seem cryptic at best in their original form. I saw it as a reminder that mathematics cannot be fully interpreted in complete isolation from the rest of the culture that produces it. To understand why Piṅgala’s algorithm takes the form it does, one must know about Sanskrit language, grammar, and aesthetics as well as the history of later scholars producing commentaries on classical texts. 

In her book Mathematics in India, Plofker includes an appendix about Sanskrit language and literature because, for example, understanding the importance of poetic meter in Sanskrit helps a student of math history understand why certain words and phrases are substituted for numbers in Sanskrit mathematics texts. An understanding of Sanskrit literature also encourages modern interpreters to be circumspect rather than overconfident about our knowledge of centuries-old Sanskrit math texts. Plofker closes the appendix on Sanskrit with this paragraph (emphasis hers):

To sum up, the path from what a medieval mathematician composing in Sanskrit intended to say to what a modern translator claims that he said is frequently long and perilous. If the author’s words were successfully preserved and transcribed by a lengthy succession of scribes, and if the commentator(s) who later encountered manuscripts of the text correctly understood and explained it, and if the editor(s) who published the text selected suitable manuscript readings in cases where one or another scribe slipped up, and if the translator had enough linguistic and technical knowledge, plus enough critical imagination, to pick the most appropriate of the possible interpretations of the resulting version of the text, then what the reader encounters will most likely be a pretty fair approximation of the original. But there are significant uncertainties present at every stage of this process, so caution is always warranted in dealing with the end result.

Plofker's talk was not actually about Piṅgala and his poetic algorithm. It was about Al-Bīrūnī, an 11th-century CE Persian polymath who visited India and wrote about, among other things, Indian mathematics scholarship. He too was trying to understand these cryptic algorithms from centuries earlier. Although this glimpse of mathematical history through his lens was a reminder of the cultural divides that can make it difficult for someone like me to understand the work of someone like Piṅgala, it also made me feel a surprising kinship across centuries with Al-Bīrūnī, who like me was trying to understand both mathematics and the people who practiced it before.