Hashing Algorithm in Ancient Indian Music System

Carnatic music system is an Indian music system that is of a very ancient origin. It is probably the oldest recorded music system in the world. It is believed that it has its origins in Sama Veda – one of the four ancient vedas. Sama Veda is actually hymns derived from Rig Veda set to musical tunes! Sama Veda is the Veda of music and melodies.

Melakartha – Base Raga in the Carnatic Music System

The Carnatic music system has 72 base ragas (called Melakartha Ragas) and thousands of derived ragas. Each raga in Indian music system depicts a mood. Some ragas are suitable to be played during sun rise, some during sunset, some produce a devotional feeling, some produce a romantic feeling, and so on. After all, music is what feelings sound like!

A base raga is a raga with 7 swaras (notes) in an octave. So there are 72 Melakartha ragas in carnatic music. Derived ragas (called Janya ragas) are ragas derived from a base raga and can have any number of notes.

The 7 notes of a raga are called Sa, Re, Ga, Ma, Pa, Dha, Ni akin to Do, Re, Mi, Fa, So, La, Ti in western classica music. However, the swara of a carnatic raga is much more complex, and the frequencies of notes are relative to each other, rather than a defined or fixed one.

The names of the 7 swaras are abbreviations of their actual names – shadja, rishabha, gandhara, madhyama, panchama, dhaivata and nishada.

Now in carnatic music, except for the first and fifth swara (Sa and Pa), other swaras have more than one form. So Ma has two forms, while other four Ri, Ga, Dha and Ni have three variants each.

If we consider the first note of an octave as Sa, then see the different positions other notes can occupy in the image below. All notes have been abbreviated to their first letter, so we have Sa(S), Ri(R), Ga(G), Ma(M), Pa(P), Dha(D) and Ni(N). Notes that can have more than on variant are indicated with the variant number, like D1, D2, N1, M2 etc.

Close up of piano keys

Now, since Ri always comes after Sa, it can be either R1 or R2 or R3. If Ri is R2, since Ga always comes after Ri, Ga can be either G2 or G3, but not G1, because it is nothing but R2.

With this simple rule that two notes cannot occupy the same place, we can see that we have 2 possible combinations for Ma, 6 possible combinations for Ri and Ga, and 6 possible combinations for Dha and Ni. So in all we have 2 x 6 x 6 = 72 possible combinations of the 7 notes here. Hence, there are 72 Melakartha ragas.

Katpayadi as a Hash table to calculate Melakartha Raga number

Each Melakartha raga has a number that ranges from 1 to 72, and a raga name. Now note that one had to remember which raga has which combination of notes, and this is a very difficult thing as we have 72 different combinations.

Hence, the ancient Indian musicians came with a simple solution for the same. They named the ragas so that the raga number could be calculated from the raga name! This was done using the Katapayadi Sankhya scheme. Read about the Katapayadi algorithm.

So, if you had a Melakartha raga name like Hanumatodi, then you can instantly calculate its raga number as 8 using the Katapayadi scheme. The first two consonants of the raga name were used in the Katapayadi scheme. In other words, Melakaratha raga names acted like a hash table, and the hash number was calculated using Katapayadi algorithm!

Then the musicians went a step further and devised another rule that allowed to calculate all the 7 notes of the raga from the raga number itself! In other words, the raga name were not just mapped via an algorithm to its number, but the number itself was first carefully selected so that it was possible to find its notes using the raga number!

Calculation of Raga notes from Raga number

The rule to find the notes were simple as follows. The notes Sa and Pa were already known since they are fixed. Now we had to find Ri, Ga, Ma, Dha and Ni.

Finding the note for Ma

Since there are two possible variations of Ma, the ragas were numbered so that the first 36 ragas had M1 and the next 36 had M2. In other words, if the raga number was <= 36, then its Ma note was M1, and if it was >36 then its Ma note is M2.

Finding the notes for Ri, Ga, Dha and Ni

The possible combinations for Ri and Ga are as follows:

  1. R1 G1
  2. R1 G2
  3. R1 G3
  4. R2 G2
  5. R2 G3
  6. R3 G3

Similarly, the possible combinations for Dha and Ni are:

  1. D1 N1
  2. D1 N2
  3. D1 N3
  4. D2 N2
  5. D2 N3
  6. D3 N3

So the rule to find each of these combinations was to

  • First divide the raga number by 6.
  • If the remainder is 0, then quotient is the RG combination number and 6 is the DN combination number.
  • If the remainder is non-zero, then quotient+1 is the RG combination number and remainder is the DN combination number.

For instance, if the raga number was 12, then since remainder is 0, RG=R1 G2 (because quotient is 2) and DN=D3 N3 (combination 6)

For instance, if the raga number was 27, then since remainder is non zero, RG=R2 G3 (because quotient is 4, so quotient+1=5) and DN=D1 N3 (because remainder is 3)

This is such an amazing piece of art work, not just some mathematics. The entire base raga system of carnatic music has been so skillfully designed that just by knowing the raga name one can instantly calculate the raga number, using which can then quickly calculate all the notes of that raga. Absolutely no need to remember all the individual 72 combinations against the raga name! Wonderful, isn’t it?


  1. an excellent entry. although i knew the KaTaPaYaDi sutra, and the way of finding the number of which melakartha raga it is, i didnt know the further use of it. good.

  2. Simply great…thanx i wonder how many times i am going to say thanx to you henceforth :)..You are just amazing….I pray you succeed in all your noble dreams….

  3. Can KaTaPaYaDi scheme be applied to Gayathri Mantra in order to derive a number that signifies some mathematical explanation about the powerful mantra?

    The reason is that in one of the slokas, I have seen in the web sites that above scheme is applied to derive the meaning of Pi =3.14

  4. Hope you know the actual vedic mathematics genius jagadguru shankaracharya of puri who was actually based from Tiruchirapalli .HIS HIGHNESS came to US for three months later died in Mumbai . I think the latest known vedic mathematician itself is this great shankaracharya.
    It seems pythagoras thereom was also originited near Kerala.
    Not only this Yoga the vedic science of physical exercise itselfs prove a lot about ancient India .

  5. The best thing about your articles is its simplicity covering the most complex information without revealing any actual complexity of the topic being discussed

  6. very beautiful.excellent find.ah to only contemplate our elders is very tough.how am i going to contemplate about the eternal truth then?

  7. I’m confused if Ma is one of two adjacent notes surely the possible combinations for Ma include the ones where Ma is the higher note leaving five notes inbetween Sa and Ma meaning a possible combination of 10?

    Or is it the case that if Ma is the higher note, Ga can not take the place of the lower possible note of Ma in any of those ragas? And the other way round.

    Thankyou very much for your time.

  8. Dear Gurudev,
    Thanks for your great thoughts on Melakartha scheme. I am also one of those intrigued by the Melakartha scheme and Katapayadi Sankhya. Ofcourse there are many more amazing things about our carnatic music system.
    I have also put down my thoughts on Melakartha scheme and their coded names. I had sent them as an article to a few musicologists in Chennai for their reflection and a possible feedback. I wish to share this article with you and hopefully I will be lucky to get your feedback.
    If it is OK with you kindly give me your email ID.
    L V Nagarajan

  9. hitxp, hello,

    I was wondering if you could possibly post a link with extra information on all of this. I am very interested in vedic mathematics but do not know where to look for good sources!

  10. its great to see ur article.. can u tell any way to identify the raaga by extracting some features of a song..it will be great to hear from you.


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.