Skip to content

← Data and representation

Did Poets in Ancient India Count in Binary?

Topic 27 · Grades 6–8 · Patterns in rhythm

Binary (Grade 6); blocks from Grade 7 (Variables and Choices); the Python parts from Grade 8 (loops and functions).

What this builds on from class
In this unit 8 sections
  1. Try it
  2. How it works
  3. Program it
  4. What your program shows
  5. Going further still
  6. Challenges
  7. Questions
  8. Where to go next

Say a line of poetry out loud and listen to its syllables. Some are quick, like the ka in kamal. Some take longer, like the maa in maalaa. Poets in Sanskrit, and in Nepali after them, call a short syllable laghu (लघु) and a long one guru (गुरु). A long syllable lasts about as long as two short ones. Books on meter mark them । for short and ऽ for long. Your Grade 10 Nepali book uses laghu and guru to explain the anushtup (अनुष्टुप्) meter of its poem उज्यालो यात्रा.

So a rhythm is a row of short and long beats. How many different rhythms are there? More than 2,000 years ago, a scholar in India set out to list them all. You can do it here first. Count a short syllable as one beat and a long one as two.

Try it

Make every rhythm

Press Short and Long to fill all 4 beats, then make another. Each new rhythm is kept below; tap one to hear it. Change Beats to try other lengths.

Beats: 4
The rhythms you make are kept here.
Beats123456
Rhythms??????

Make a rhythm that fills exactly 4 beats, with Short (one beat) and Long (two beats).

Try 1, 2 and 3 beats first. Then guess: how many different rhythms fill 6 beats?

Pick an answer to see why.

You need: binary numbers, from Grade 6 (Zeros and Ones). The blocks use variables and repeat, from Grades 6 and 7; the Python parts need loops and functions, from Grade 8.

Try it

Use the rhythm maker above.

  • a.Make every rhythm of 1 beat, 2 beats and 3 beats. The table under the rhythm maker fills in a count once you have found all of a length.
  • b.Now do 4 beats and 5 beats. Write the counts down in a row: 1, 2, 3, …
  • c.Tap a few kept rhythms to hear them. Do they all take the same time?

The first list of rhythms

The scholar was Pingala (पिङ्गल), who lived in India in about the 3rd or 2nd century BCE. His Chandahshastra (छन्दःशास्त्र) is the oldest book on Sanskrit meter that survives. It is written in sutras, rules of a few words each, short enough to learn by heart. Near the end, a few sutras explain how to write down every rhythm of a given number of syllables (not beats: every rhythm of 3 syllables, whatever its length). The list is called the prastara (प्रस्तार), “the spreading out”.

The first sutras read द्विकौ ग्लौ (dvikau glau) and मिश्रौ च (mishrau cha), only four words. As they are usually read, one syllable gives two rhythms, g or l, a guru or a laghu; mix them for two syllables; then mix each of those with g and with l for three. And then वसवस्त्रिकाः (vasavas trikah): “the threes are eight”, vasavah being a word that stands for eight. Here is that list, as the widget draws it.

Try it

Pingala’s list

Press Next row to go down the list. Tap a syllable to change it and see which row you land on. Then try 4 syllables.

Syllables: 3

Row 5 of 8 in Pingala’s list: long, long, short. Look down the list: how often does each place change?

Look down the list for 3 syllables. The first syllable changes on every row, the second every two rows, the third after four. Have you seen that pattern before?

In Pingala’s list of 4 syllables, how many rows does the fourth syllable stay long before it changes?

Pick an answer to see why.

How it works

Pingala’s list is binary counting

In Grade 6 you counted in binary on cards or bits Grade 6: Counting in binary: the ones bit changes on every count, the twos bit every second count, the fours bit every fourth. Pingala’s list changes in the same way. Write a 1 for each short syllable and a 0 for each long one, and give the places the worths 1, 2, 4, 8 from the left. Press Show 1s and 0s in the widget above to see it.

Row Rhythm 1s and 0s, as Pingala writes them Its binary number, as we write it Its value
1 ऽ ऽ ऽ 0 0 0 000 0
2 । ऽ ऽ 1 0 0 001 1
3 ऽ । ऽ 0 1 0 010 2
4 । । ऽ 1 1 0 011 3
5 ऽ ऽ । 0 0 1 100 4
6 । ऽ । 1 0 1 101 5
7 ऽ । । 0 1 1 110 6
8 । । । 1 1 1 111 7

The rows count 0 to 7 in binary, with two differences from the way you write it in Grade 6:

  • The ones place is on the left: the first syllable spoken changes fastest. Turn the 1s and 0s round and you have ordinary binary.
  • The rows start at 1, not 0, so the row number is always one more than the binary number.

Which rhythm is row 6 of Pingala’s list of 3 syllables?

Pick an answer to see why.

Rules for jumping to any row

A list written in sand can get rubbed out, and one scholar thinks that is why Pingala also gave a rule to find the rhythm in any row without writing the list. It is called nashta (नष्ट), “lost”:

  • If the row number is even, write a short syllable and halve the number.
  • If it is odd, write a long syllable, add one and halve.
  • Do this once for each syllable.

For row 5 of 3 syllables: 5 is odd, so long, and (5 + 1) ÷ 2 = 3. 3 is odd, so long, and (3 + 1) ÷ 2 = 2. 2 is even, so short. The rhythm is ऽ ऽ ।, as in the table. In Grade 6 you found a number’s bits from the biggest place value down Grade 6: Writing a number in binary; halving finds them from the ones place up, which is why Pingala’s rhythm comes out ones place first. (Adding one before halving an odd number takes care of the rows starting at 1.)

The rule the other way, from a rhythm to its row, is called uddishta (उद्दिष्ट), “pointed out”. Pingala’s way doubles and takes away. Around 850, the mathematician Mahavira, in his Ganitasarasangraha, gave a simpler one: write 1, 2, 4, 8… above the syllables, add the numbers above the short ones, and add one. That is reading a binary number by its place values, and Jayant Shah, a mathematician who wrote a history of these rules, calls it “the closest Indian mathematicians came to inventing binary numbers”.

And to go from a row to the next one, the Vrittaratnakara of Kedara Bhatta, a book on meter written by the early 1100s, gives this rule (Jayadeva’s, before 900, has it too): under the first long syllable put a short one; copy the syllables after it; make every syllable before it long. On । । ऽ (row 4) the first long one is the third: it becomes short, and the two before it long, ऽ ऽ । (row 5). That is adding one in binary, 1 1 0 to 0 0 1, the 1s turning to 0s as the carry passes. The Next row button does exactly this.

Why the counts add up

Now back to beats, where a short syllable is one beat and a long one two. This kind of counting, by length in beats, is called matra (मात्रा), the time a syllable takes to say. Why do the counts of rhythms go 1, 2, 3, 5, 8?

Think about how a rhythm of 5 beats ends. Its last syllable is either short or long.

  • If it ends short, the syllables before it fill 4 beats. They can be any of the 5 rhythms of 4 beats. So there are 5 of these.
  • If it ends long, the syllables before it fill 3 beats: any of the 3 rhythms of 3 beats. So there are 3 of these.
Ends short: a rhythm of 4 beats, then । Ends long: a rhythm of 3 beats, then ऽ
ऽ ऽ । । ऽ ऽ
। । ऽ । ऽ । ऽ
। ऽ । । । । । ऽ
ऽ । । ।
। । । । ।

Every rhythm of 5 beats is in exactly one column, so there are 5 + 3 = 8. The same thinking works for any length: the rhythms of n beats are the rhythms of n − 1 beats with a short syllable added, plus the rhythms of n − 2 beats with a long one added. Each count is the sum of the two before it.

There are 21 rhythms of 7 beats and 34 of 8 beats. How many of the rhythms of 9 beats end in a long syllable?

Pick an answer to see why.

Program it

With blocks

Here is Pingala’s nashta rule as a block program: type a row number, and it says the rhythm in that row of the list of 4 syllables. It builds the rhythm as text, an L for each short syllable (laghu) and a G for each long one (guru), the letters books on Pingala use.

Try it

Pingala’s rule

Press Run and type 5. Watch the trace table: the row number halves (after adding one when it is odd), and a G or an L is joined on at each step. Then press Test.

What the program says and asks appears here.

row – no value
rhythm – no value
StepBlockrowrhythm
TestShould sayIt said
Row 1GGGG
Row 2LGGG
Row 5GGLG
Row 11GLGL
Row 16LLLL

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

In Python (from Grade 8)

The same rule in Python is a function. It takes the row number and the number of syllables, and gives back the rhythm as a string of L and G. row // 2 halves a whole number.

def rhythm(row, syllables):    text = ''    for i in range(syllables):        if row % 2 == 0:            text = text + 'L'            row = row // 2        else:            text = text + 'G'            row = (row + 1) // 2    return text for row in range(1, 9):    print(row, rhythm(row, 3))

Output

1 GGG2 LGG3 GLG4 LLG5 GGL6 LGL7 GLL8 LLL

That is Pingala’s list for 3 syllables, written by a rule of 2,000 years ago. Now put the list beside binary. This program writes each rhythm’s 1s and 0s turned round (a 1 for each L, with the first syllable on the right), and beside it the binary number for one less than the row, worked out the Grade 6 way, by remainders.

def rhythm(row, syllables):    text = ''    for i in range(syllables):        if row % 2 == 0:            text = text + 'L'            row = row // 2        else:            text = text + 'G'            row = (row + 1) // 2    return text def binary(number, digits):    text = ''    for i in range(digits):        text = str(number % 2) + text        number = number // 2    return text same = 0for row in range(1, 17):    r = rhythm(row, 4)    turned = ''    for syllable in r:        if syllable == 'L':            turned = '1' + turned        else:            turned = '0' + turned    print(row, r, turned, binary(row - 1, 4))    if turned == binary(row - 1, 4):        same = same + 1print(same, 'of 16 rows are binary counting')

Output

1 GGGG 0000 00002 LGGG 0001 00013 GLGG 0010 00104 LLGG 0011 00115 GGLG 0100 01006 LGLG 0101 01017 GLLG 0110 01108 LLLG 0111 01119 GGGL 1000 100010 LGGL 1001 100111 GLGL 1010 101012 LLGL 1011 101113 GGLL 1100 110014 LGLL 1101 110115 GLLL 1110 111016 LLLL 1111 111116 of 16 rows are binary counting

Every rhythm of so many beats

Pingala’s lists can also find every rhythm of a number of beats. A rhythm of 4 beats has between 2 syllables (two longs) and 4 (four shorts). So go through his lists for 1, 2, 3 and 4 syllables, add up each rhythm’s beats, and keep the ones that come to exactly 4.

def rhythm(row, syllables):    text = ''    for i in range(syllables):        if row % 2 == 0:            text = text + 'L'            row = row // 2        else:            text = text + 'G'            row = (row + 1) // 2    return text def beats(r):    total = 0    for syllable in r:        if syllable == 'L':            total = total + 1        else:            total = total + 2    return total def rhythms_of(n):    found = []    for syllables in range(1, n + 1):        for row in range(1, 2 ** syllables + 1):            r = rhythm(row, syllables)            if beats(r) == n:                found.append(r)    return found print(rhythms_of(4))print('beats  rhythms')for n in range(1, 11):    print(n, len(rhythms_of(n)))

Output

['GG', 'LLG', 'LGL', 'GLL', 'LLLL']beats  rhythms1 12 23 34 55 86 137 218 349 5510 89

Hear them

The page’s Python has its own sound module, which plays a list of numbers from −1 to 1 as a sound (on a computer you would need a sound library instead). Each syllable here is a note that dies away: a short one higher, for one beat, a long one lower, for two. This program plays the five rhythms of 4 beats that the last one found, with a beat of quiet after each.

import mathfrom sound import play RATE = 8000      # numbers a secondBEAT = 0.18      # seconds def note(syllable):    if syllable == 'L':        pitch = 587        fade = 14        length = BEAT    else:        pitch = 392        fade = 3        length = 2 * BEAT    samples = []    for i in range(int(length * RATE)):        t = i / RATE        samples.append(0.6 * math.exp(-fade * t) * math.sin(2 * math.pi * pitch * t))    return samples samples = []for r in ['GG', 'LLG', 'LGL', 'GLL', 'LLLL']:    for syllable in r:        samples = samples + note(syllable)    samples = samples + [0] * int(BEAT * RATE)play(samples, RATE)print(round(len(samples) / RATE, 2), 'seconds')

Output

4.5 seconds

Every rhythm takes the same 4 beats, filled five ways. Change the list to play the 8 rhythms of 5 beats (make BEAT smaller so they fit in 5 seconds).

What your program shows

Your program went through Pingala’s lists and counted: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. Nothing in it adds two counts together. It only tries rhythms and keeps those of the right length, yet every count is the sum of the two before it. Knowing why gives a far quicker program, which makes no rhythms at all: it starts from 1 and 2 and adds.

before = 1   # rhythms of 1 beatnow = 2      # rhythms of 2 beatsfor n in range(3, 31):    after = now + before    before = now    now = afterprint('Rhythms of 30 beats:', now) rows = 0for syllables in range(1, 31):    rows = rows + 2 ** syllablesprint('Rows of Pingala’s lists to try for them:', rows)

Output

Rhythms of 30 beats: 1346269Rows of Pingala’s lists to try for them: 2147483646

So there are 1,346,269 rhythms of 30 beats. The listing program would try over two thousand million rows to find them; the adding program does 28 additions, because it knows why the counts grow.

Going further still

Pingala’s quick count. How many rhythms of 8 syllables are there? Two choices for each syllable: 2 × 2 × … eight times, 256. Other sutras of Pingala’s give a quicker way: while the number of syllables isn’t 0, halve it if it is even and note “square”, or take one away if it is odd and note “double”. Then start from 1 and do the notes backwards. For 8: 8 → 4 → 2 → 1 → 0, so square, square, square, double; backwards, 1 doubled is 2, squared 4, squared 16, squared 256. Four steps instead of seven multiplications. Write it as a Python function and try it for 64 syllables. (Computers still raise numbers to powers this way.)
Rhythms with so many long syllables. Of the 16 rhythms of 4 syllables, how many have no long syllable, one, two, three, four? Count them in Pingala’s list. Later commentators set these counts out in a triangle, the meru prastara (मेरुप्रस्तार), “the mountain”, known in Europe as Pascal’s triangle: see How Many Ways Can You Walk to School?.

Challenges

★ Count the rhythms (blocks)

Finish the program so that it says how many rhythms fill the typed number of beats (for 5 beats it says 8), by the adding rule, without making any rhythms. Then press Test.

Try it

★ Count the rhythms

What the program says and asks appears here.

beats – no value
before – no value
now – no value
done – no value
StepBlockbeatsbeforenowdone
TestShould sayIt said
1 beat1
2 beats2
5 beats8
10 beats89
25 beats121393

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

Hint
Another hint
Answer

★★ Which row is it? (Python, from Grade 8)

Write row_number(rhythm): the row of Pingala’s list where a rhythm of L and G is, counting from 1. For 'GGL' it gives 5, and for 'LLLL' 16. (This is Mahavira’s rule.)

def row_number(rhythm):    # rhythm is a string of 'L' and 'G'    return 1 print(row_number('GGL'))

Try it

★★ Which row is it?

  1. GGL row_number('GGL') Should give 5 Yours —
  2. LLLL row_number('LLLL') Should give 16 Yours —
  3. One long row_number('G') Should give 1 Yours —
Tests passed
— / 9
Challenge
★★ solved

Write your row_number in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Answer

★★★ The millionth rhythm (Python, from Grade 8)

Put every rhythm of beats beats in alphabetical order, as Python’s sorted() does (G comes before L). For 4 beats that is GG, GLL, LGL, LLG, LLLL. Write rhythm_at(beats, k) that gives the k-th of them, counting from 1, without making the list: the tests ask about rhythms of 40 beats, of which there are 165,580,141.

def rhythm_at(beats, k):    # the k-th rhythm of this many beats, in alphabetical order    return '' print(rhythm_at(4, 2))

Try it

★★★ The millionth rhythm

  1. 4 beats, the 2nd rhythm_at(4, 2) Should give 'GLL' Yours —
  2. 4 beats, the 1st rhythm_at(4, 1) Should give 'GG' Yours —
  3. 4 beats, the last rhythm_at(4, 5) Should give 'LLLL' Yours —
Tests passed
— / 10
Challenge
★★★ solved

Write your rhythm_at in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Another hint
Answer

Questions

In Pingala’s list of 3 syllables, which row is । । । (all short)?

Pick an answer to see why.

a.

Use Pingala’s nashta rule to find the rhythm in row 11 of the list of 4 syllables. Show each step.

Answer
b.

There are 55 rhythms of 9 beats and 89 of 10 beats. How many are there of 11 beats and of 12 beats? Explain why, without listing them.

Answer
c.

A friend says: “Pingala’s list is just the binary numbers, so Pingala invented computers.” What is right in this, and what isn’t?

Answer

Where to go next

Found a mistake? Please tell us.