The katapayadi system: how Sanskrit verses encode numbers, with a Python decoder
The katapayadi system from the Sanskrit sources: the Sadratnamala rule, pi to 17 places decoded syllable by syllable, raga numbers and a Python decoder.
bhadrāmbudhisiddhajanmagaṇitaśraddhā sma yad bhūpagīḥ is a line of Sanskrit verse from the Sadratnamala of 1819, and it is also pi to 17 decimal places. It is written in the katapayadi system: each consonant stands for a digit from 0 to 9, and the digits are read from right to left. Kerala astronomers used it from at least 683 CE to put long numbers into verse, so a table of the moon's motion or a value of pi could be memorized as a line of poetry.
I took the verse that states the rule, translated it word by word, decoded attested examples from the sources, and wrote a Python decoder for IAST and Devanagari, with tests (code on GitHub). The series is Ancient algorithms, implemented.
The problem it solves
Sanskrit astronomy was written in verse, and verse has a fixed rhythm. A number like 1,712,210 does not fit a meter as digits, and it is hard to remember. Indian authors had 2 ways to put numbers into words, as S. R. Sarma (2012) describes them:
- Word numerals (bhutasankhya). Each digit is a word for something that comes in that number: "moon" for 1, "eyes" or "hands" for 2. These were used across India and in Southeast Asia. Sarma names Pingala's Chandahsutra as the earliest text that uses them extensively.
- Letter numerals (katapayadi). Each consonant is a digit. This is the Kerala system.
Both are place-value systems that start from the units place, so both are read from right to left. Letters have 1 advantage: every syllable is a digit, so a phrase can carry a number and a meaning at once. Sarma's example is naṭanara, "actor-man", which spells 2010.
The digits come from the alphabet in 4 runs, which give the system its name: the runs begin with ka, ṭa, pa and ya.
| Digit | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|
| ka run | ka | kha | ga | gha | ṅa | ca | cha | ja | jha | ña |
| ṭa run | ṭa | ṭha | ḍa | ḍha | ṇa | ta | tha | da | dha | na |
| pa run | pa | pha | ba | bha | ma | |||||
| ya run | ya | ra | la | va | śa | ṣa | sa | ha | ||
| vowels | a vowel on its own |
The table follows Sarma (2012, p. 39). The ka and ṭa runs are 10 letters each, so they cover 1 to 9 and 0. The pa run has 5 letters and the ya run 8.
Who used it, and when
- Haridatta, Grahacaranibandhana, Kerala, 683 CE. Sarma (2012) calls this "the first positive and datable occurrence". It introduced the Parahita system of astronomy. Sundararaja, a commentator of c. 1475 to 1500, says Haridatta and others adopted the system.
- Shankaranarayana (c. 825 to 900), commentary on Bhaskara I's Laghubhaskariya. The name kaṭapayādi is attested here, according to Kunjunni Raja, whom Sarma cites.
- The candravakyas ("moon sentences"), 248 phrases giving the moon's daily motion. Tradition ascribes them to Vararuci, a legendary figure placed in the 4th century. Sarma quotes David Pingree that "the earliest attested epoch of the lunar vakyas is 1184".
- Kerala dates. Kerala authors dated their works by the number of days since the start of the Kali era (18 February 3102 BCE, in the Julian calendar), written in katapayadi. K. V. Sarma used these chronograms to date the Kerala astronomers whose work appears in the Madhava post.
- Sankaravarman, Sadratnamala, 1819. The fullest statement of the rule, below.
- Outside Kerala. Sarma (2012) traces it to Tamil Nadu, where Venkatamakhin used it for raga names (1660), and to Jain authors in Karnataka, such as Nemicandra in the 10th century. In northern India it was known but, he found, almost never used in scientific work.
Sarma notes that Kerala, where the system was used for over 1,100 years, has no definition older than the 1819 verse below. Definitions from elsewhere are older: Aryabhata II gives one for his own variant in the Mahasiddhanta (c. 950), and Todarmal quotes one in his commentary of 1762, which tells the reader to ignore the first consonant of a cluster.
The rule, in the Sadratnamala
The verse is Sadratnamala 3.3, on page 22 of K. V. Sarma's edition in the Indian Journal of History of Science 36 (2001). I checked it against the printed page. The meter is anushtubh.
नञावचश्च शून्यानि संख्याः कटपयादयः ।
मिश्रे तूपान्त्यहल् संख्या न च चिन्त्यो हलस्वरः ॥ ३ ॥
nañāv acaś ca śūnyāni saṃkhyāḥ kaṭapayādayaḥ |
miśre tūpāntyahal saṃkhyā na ca cintyo hal asvaraḥ || 3 ||
Word by word
2 of the words come from Panini's grammar. Ac is Panini's short name for all the vowels and hal his short name for all the consonants, so the verse uses a grammarian's vocabulary.
| Sanskrit | Parts | Meaning |
|---|---|---|
| nañau | na + ña | na and ña |
| acaḥ | ac | the vowels |
| ca | and | |
| śūnyāni | śūnya | are zeros |
| saṃkhyāḥ | saṃkhyā | are the numbers |
| kaṭapayādayaḥ | ka + ṭa + pa + ya + ādi | (the consonants) beginning with ka, ṭa, pa and ya |
| miśre | miśra | in a mixture, that is, a consonant cluster |
| tu | but | |
| upāntyahal | upāntya + hal | the next-to-last sound, a consonant |
| saṃkhyā | is the number | |
| na ca cintyaḥ | na, ca, cintya | and is not to be counted |
| hal asvaraḥ | hal, a + svara | a consonant without a vowel |
My translation
Ña, na and the vowels are zeros. The consonants beginning with ka, ṭa, pa and ya are the numbers. In a cluster, the consonant next to the vowel is the number. A consonant without a vowel is not counted.
"Next-to-last" makes sense once you see a syllable as a run of sounds ending in a vowel. In kṣa, the sounds are k, ṣ, a. The last is the vowel, and the next-to-last is ṣ, so kṣa is 6.
What the verse leaves open
- Which way to read. The verse never says the first syllable is the units digit. Sarma (2012) cites the maxim aṅkānāṃ vāmato gatiḥ, "the digits go to the left", and shows that Kerala texts always read right to left. Aryabhata II's Mahasiddhanta used a left-to-right version of the system, and Sarma finds no other text that followed it.
- Anusvara and visarga. The nasal ṃ and the final breath ḥ are not consonants in the alphabet's runs, and the examples treat them as worth nothing. The code does the same.
- Where a word ends. Sanskrit runs words together when recited, so a consonant at the end of one word joins the vowel at the start of the next. In Devanagari a final consonant without a vowel carries a mark, the virama, and the code skips it. In IAST the code lets a final consonant join the next vowel.
- Letters outside the table. Sarma reports that the Malayalam consonant ḻa was given the value 7. The code reads only the letters in the table and rejects the rest, including ḷ, which IAST uses both for a vowel and for the consonant ḷa, since no source I read gives either a value. Devanagari ऌ is rejected for the same reason, so the 2 scripts agree.
Decoding pi from the Sadratnamala
Sadratnamala 4.2 (page 26 of Sarma's 2001 edition) gives the circumference of a circle whose diameter is a parardha, 1017. The second half of the verse reads:
एवं चात्र परार्धविस्तृतिमहावृत्तस्य नाहोऽक्षरैः
स्याद् भद्राम्बुधिसिद्धजन्मगणितश्रद्धा स्म यद् भूपगीः ॥
evaṃ cātra parārdhavistṛtimahāvṛttasya nāho 'kṣaraiḥ
syād bhadrāmbudhisiddhajanmagaṇitaśraddhā sma yad bhūpagīḥ ||
In this way, the circumference of a great circle whose diameter is a parardha is, in letters, bhadrāmbudhisiddhajanmagaṇitaśraddhā sma yad bhūpagīḥ.
The word akṣaraiḥ, "in letters", tells the reader that a katapayadi number follows. Syād, "would be", is the verb. The number starts at bha. Split it into syllables and take the last consonant of each:
| # | Syllable | Counted consonant | Digit |
|---|---|---|---|
| 1 | bha | bh | 4 |
| 2 | drā | r | 2 |
| 3 | mbu | b | 3 |
| 4 | dhi | dh | 9 |
| 5 | si | s | 7 |
| 6 | ddha | dh | 9 |
| 7 | ja | j | 8 |
| 8 | nma | m | 5 |
| 9 | ga | g | 3 |
| 10 | ṇi | ṇ | 5 |
| 11 | ta | t | 6 |
| 12 | śra | r | 2 |
| 13 | ddhā | dh | 9 |
| 14 | sma | m | 5 |
| 15 | ya | y | 1 |
| 16 | dbhū | bh | 4 |
| 17 | pa | p | 1 |
| 18 | gīḥ | g | 3 |
Written in order the digits are 423979853562951413. Reverse them, units first, and you get 314159265358979324. Divide by the diameter, 1017:
Pi is 3.14159265358979323846..., so the Sadratnamala gives pi rounded to 17 decimal places. Charles Whish read the same verse in 1834 and printed the same 18 digits.
Look at syllables 2, 12 and 16. Drā has the cluster d-r, and only r counts: 2, where d would give 8. Śra has ś-r, and r gives 2, where ś would give 5. Dbhū has d-bh, and bh gives 4, where d would give 8. Reading the first consonant of each cluster instead changes the number.
More numbers from the sources
Each of these comes from a source I read, and each one decodes to the stated value in the tests:
| Phrase | Source | Value |
|---|---|---|
| anantapura | Whish 1834, p. 511: minutes in a full circle | 21,600 |
| anūnanūnnānananunnanityaiḥ | Karanapaddhati, as printed by Whish 1834, p. 511 | 10,000,000,000 |
| caṇḍāṃśucandrādhamakumbhipālaiḥ | same verse | 31,415,926,536 |
| gīr naḥ śreyaḥ | first moon sentence, Sarma 2012 | 1203, that is 12° 03' |
| dhenavaḥ śrīḥ | second moon sentence | 2409, that is 24° 09' |
| āyurārogyasaukhyam | last words of Narayana Bhattatiri's Narayaniyam | 1,712,210 days |
| vijñānadīptāśayaḥ | Suryadeva Yajvan's birth date | 1,568,004 days |
The Karanapaddhati verse divides the circumference by 31,415,926,536 and multiplies by 10,000,000,000 to get the diameter: pi to 10 decimal places, in 2 phrases.
The Narayaniyam line means "long life, health and happiness", a blessing at the end of a devotional poem. It is also a date: 1,712,210 days since the start of the Kali era. Sarma (2012) gives the offset between this day count and the Julian day number used by astronomers today, 588,466. Add it and convert, and the test gets 8 December 1586 in the Gregorian calendar, the date Sarma gives.1
How the katapayadi system works
Katapayadi is ordinary base-10 place value with a different alphabet for the digits. A phrase with syllables s1, s2, ..., sn and digit values d1, d2, ..., dn stands for
So decoding is: find the syllables, map each to a digit, and read the digits backwards. The only part that needs care is the first step, splitting the text into syllables, which is what the verse's rules about clusters and bare consonants are for.
Encoding is not unique. 8 of the 10 digits have 3 or 4 consonants to choose from; 9 and 0 have 2 each, and 0 can also be a bare vowel. Any vowel can follow the consonant. For example, a 7-digit number made only of digits with 4 consonants, written with only the 5 vowels a, i, u, e and o, has 20^7, about 1.3 billion, spellings, before counting clusters. That freedom is how a poet finds a spelling that scans and means something, like āyurārogyasaukhyam.
The Python
A decoder for IAST in about 30 lines:
DIGITS = {
**dict.fromkeys(["k", "ṭ", "p", "y"], 1),
**dict.fromkeys(["kh", "ṭh", "ph", "r"], 2),
**dict.fromkeys(["g", "ḍ", "b", "l"], 3),
**dict.fromkeys(["gh", "ḍh", "bh", "v"], 4),
**dict.fromkeys(["ṅ", "ṇ", "m", "ś"], 5),
**dict.fromkeys(["c", "t", "ṣ"], 6),
**dict.fromkeys(["ch", "th", "s"], 7),
**dict.fromkeys(["j", "d", "h"], 8),
**dict.fromkeys(["jh", "dh"], 9),
**dict.fromkeys(["ñ", "n"], 0),
}
VOWELS = {"ai", "au", "a", "ā", "i", "ī", "u", "ū", "ṛ", "ṝ", "e", "o"}
def decode(text: str) -> int:
"""The number a katapayadi phrase in IAST spells, read from right to left."""
text = text.lower()
digits, consonant, i = [], None, 0
while i < len(text):
two, one = text[i : i + 2], text[i]
if two in VOWELS or one in VOWELS: # a vowel closes the syllable
digits.append(0 if consonant is None else DIGITS[consonant])
consonant = None
i += 2 if two in VOWELS else 1
elif two in DIGITS or one in DIGITS: # the last consonant before the vowel counts
consonant = two if two in DIGITS else one
i += len(consonant)
elif one in " -'ṃḥ": # spaces, avagraha, anusvara and visarga count for nothing
i += 1
else:
raise ValueError(f"unknown letter {one!r} in {text!r}")
return int("".join(str(d) for d in reversed(digits)))
print(decode("bhadrāmbudhisiddhajanmagaṇitaśraddhā sma yad bhūpagīḥ")) # 314159265358979324
print(decode("āyurārogyasaukhyam")) # 1712210
The repo version also reads Devanagari, where the virama marks a consonant without a vowel. Like the short version, it rejects letters outside the table. It has an encoder that writes one spelling of any number, and reads melakarta numbers. The tests check:
- every example in the table above, plus the Sadratnamala pi verse in both IAST and Devanagari;
- the Narayaniyam chronogram against Sarma's date, 8 December 1586;
- the 4 melakarta numbers Sarma gives, and 1 name, Cakravākam, where the rule gives 26 for melakarta 16;
- the rules of the verse one at a time: a bare vowel is 0, only the last consonant of a cluster counts, a final consonant and ṃ and ḥ count for nothing;
- property tests with Hypothesis: encoding then decoding returns any number up to 1040, and random spellings built from the table, with random clusters and vowels, decode to the digits they were built from.
In music: the 72 melakarta ragas
Carnatic music arranges its parent scales, the melakarta ragas, in a fixed list of 72. The first 2 syllables of each name give its place in the list. Sarma's 4 examples decode by the rule above:
| Raga | First 2 syllables | Digits | Reversed | Number |
|---|---|---|---|---|
| Kanakāṅgī | ka, na | 1, 0 | 01 | 1 |
| Rūpavatī | rū, pa | 2, 1 | 12 | 12 |
| Ṣaṇmukhapriyā | ṣa, ṇmu | 6, 5 | 56 | 56 |
| Rasikapriyā | ra, si | 2, 7 | 72 | 72 |
melakarta_number in the repo reproduces these 4. The rule as the Sadratnamala states it does not fit every name. In Cakravākam, Divyamaṇi, Viśvambharī, Śyāmalāṅgī, Citrāmbarī and Jyotisvarūpiṇī the second or first syllable is a cluster, and the standard numbers (16, 48, 54, 55, 66 and 68) need the first consonant of the cluster to count; the Sadratnamala rule gives 26, 18, 44, 51, 26 and 61. Siṃhendramadhyamam (57) counts the anusvara as m, and the numbers of Naṭabhairavī (20) and Ṛṣabhapriyā (62) depend on the spelling. I took the standard numbers from Wikipedia's list of the 72, the only full list I read. Venkatamakhin listed all 72 scales his rules allow, in 12 groups (cakras) of 6 (Sarma 2012), so the number fixes the scale.
Against modern notation
Katapayadi and our decimal digits encode the same thing, a number in base 10. Written decimal puts the most significant digit first; katapayadi puts the units first. The other differences are in what else the notation does:
- Redundancy. Decimal has 1 symbol per digit. Katapayadi has 2 to 4 consonants per digit, and each can take any vowel, so the same number has many spellings. That redundancy is what lets a number fit a meter and carry a meaning.
- Order. The units-first order matches the way a computer stores an integer in little-endian memory, and the order in which column addition works through the digits.
What is debated
- Who invented it. Unknown. Haridatta (683) is the first datable user. Sundararaja links the system to Haridatta. Suryadeva, born on Kali day 1,568,004 (4 February 1192 in the Julian calendar), argued that Aryabhata (499) must have known it; Sarma reports the claim without endorsing it.
- How old it is. Claims of great antiquity exist, before Pingala or in the Mahabharata. Sarma (2012) calls one of them "highly speculative" and rejects them on the evidence: if the system were that old, examples would turn up across India, and they do not.
- Who named the ragas this way. Sarma (2012) credits the katapayadi naming of the 72 melakarta ragas to Venkatamakhin's Caturdandiprakasika (1660). Other accounts say the names with katapayadi prefixes came about a century after Venkatamakhin and were standardized by Govindacharya.
- How many variants. B. Datta and A. N. Singh listed 4 variants in 1935. Sarma (2012) shows that only 2 are real: the Kerala system, read right to left, and Aryabhata II's left-to-right version, used in 1 text.
Key takeaways
- Katapayadi writes base-10 numbers as Sanskrit syllables. The consonants from ka, ṭa, pa and ya count 1 upward; ña, na and bare vowels are 0; only the last consonant of a cluster counts; the digits read right to left.
- The first datable use is Haridatta's Grahacaranibandhana (Kerala, 683). The fullest statement of the rule is Sadratnamala 3.3 (1819).
- Sadratnamala 4.2 encodes pi rounded to 17 decimal places in 18 syllables.
- The first 2 syllables of a melakarta raga's name give its number. Sarma's 4 examples follow the Sadratnamala rule; names such as Cakravākam (16) need the first consonant of a cluster instead.
- The decoder, encoder and tests are in ancient-algorithms. The Madhava post uses word numerals from the same Kerala texts.
Sources
- S. R. Sarma, "The Kaṭapayādi system of numerical notation and its spread outside Kerala". Revue d'histoire des mathématiques 18, 2012, pp. 37 to 66.
- Sankaravarman, Sadratnamala, edited by K. V. Sarma. Indian Journal of History of Science 36 (3 to 4), 2001. Scan on archive.org. Verses 3.3 (p. 22) and 4.2 (p. 26).
- C. M. Whish, "On the Hindú quadrature of the circle". Transactions of the Royal Asiatic Society of Great Britain and Ireland 3 (1834), pp. 509 to 523.
Footnotes
-
The test converts with Python's
date.fromordinal, which uses the Gregorian calendar for all dates. Sarma gives Suryadeva's birth as Kali day 1,568,004 and as 4 February 1192, labeled Gregorian; his text also says "b. 1191". With Sarma's offset, that day count is 4 February 1192 in the Julian calendar, the calendar in use then, and 11 February in the Gregorian. ↩
Common questions
What is the katapayadi system?↓
Katapayadi (kaṭapayādi) is a way of writing numbers as Sanskrit syllables. The consonants starting from ka, ṭa, pa and ya stand for 1, 2, 3 and so on; ña, na and a vowel on its own stand for 0. Each syllable gives 1 digit, from its last consonant, and the digits are read from right to left, units first. It was used in Kerala from at least 683 CE to write astronomical tables and dates in verse.
What is katapayadi in music?↓
In Carnatic music the first 2 syllables of each of the 72 melakarta raga names give its number, read right to left. Kanakangi starts ka (1), na (0), so it is melakarta 1; Rasikapriya starts ra (2), si (7), so it is 72. Some names follow a different cluster rule: Cakravakam is 16, which needs the first consonant of kra to count. S. R. Sarma (2012) credits the naming to Venkatamakhin's Caturdandiprakasika (1660). Wikipedia's Melakarta article, citing A. Sundaram Iyer, places it about a century later; I have not found a scholarly source for that view.
Who invented the katapayadi system?↓
Nobody knows. The first use that can be dated is in Haridatta's Grahacaranibandhana, written in Kerala in 683 CE, and a 15th-century commentator, Sundararaja, links the system with Haridatta. Kerala tradition ascribes the older moon sentences (candravakyas) to Vararuci, whom legend places in the 4th century, but S. R. Sarma (2012) finds no firm evidence for that date.
Which way do you read katapayadi numbers?↓
From right to left. The first syllable is the units digit, the second the tens, and so on, so naṭanara (na 0, ṭa 1, na 0, ra 2) is 2010. The rule-giving verse in the Sadratnamala does not say this; the convention comes from the maxim aṅkānāṃ vāmato gatiḥ, 'the digits go to the left'.
What is katapayadi sankhya?↓
Sankhya means 'number', so katapayadi sankhya is 'katapayadi numbering', another name for the same system. In Malayalam it is also called paralpperu, a name tied to the Kerala practice of computing with sea shells.