Category Archives: Algorithms

Radix Conversion of Fractional Numbers

By Chandrahas M. Halai

 

One of the most significant things that ancient India has given to the world of mathematics is the place value number system. In this system we can use any number as our base. Ten is the preferred base for the obvious reason that we have ten fingers in our two hands.

A quantity in place value number system can be represented as a polynomial in the chosen base number. This is as given below:


Refer my earlier articles:

https://chandrahasblogs.wordpress.com/2020/04/22/how-pingala-created-the-binary-number-system/

https://chandrahasblogs.wordpress.com/2020/05/18/pingalas-algorithm-for-binary-conversion/

 




 

As an example, let us convert the decimal number 235 into binary.

Since, we want to convert a decimal number 235 into binary we will divide it by 2.

Step 1) 235/2

Quotient = 117, remainder = 1

Thus, 1 will be the digit in the lowest value place.

Step 2) 117/2

Quotient = 58, remainder = 1

Step 3) 58/2

Quotient = 29, remainder = 0

Step 4) 29/2

Quotient = 14, remainder = 1

Step 5) 14/2

Quotient = 7, remainder = 0

Step 6) 7/2

Quotient = 3, remainder = 1

Step 7) 3/2

Quotient = 1, remainder = 1

Step 8) 1/2

Quotient = 0, remainder = 1

Since here, the quotient is 0, the division process comes to an end.

Thus, the binary representation of the decimal number 235 is 1110 1011.

 

Now, how can we convert the base of fractional quantities?



 

 




To get the digits in the lower value places go on repeating the above procedure. The process stops when the multiplicand becomes zero.


 

As an example, let us convert the fractional part of the decimal number 235.671875 into binary.

Since, we want to convert the fractional part of the decimal number into binary we will multiply it by 2.

Step 1) 0.671875 * 2 = 1.34375

Hence, the first digit after binary point = 1

Step 2) 0.34375 * 2 = 0.6875    

Hence, the second digit after binary point = 0

Step 3) 0.6875 * 2 = 1.375

Hence, the third digit after binary point = 1

Step 4) 0.375 * 2 = 0.75

Hence, the fourth digit after binary point = 0

Step 5) 0.75 * 2 = 1.5

Hence, the fifth digit after binary point = 1

Step 6) 0. 5 * 2 = 1.0

Hence, the sixth digit after binary point = 1

Since here, the multiplicand is 0, the division process comes to an end.

 

Thus, the binary representation of the decimal number 235.671875 is 11101011.101011.

 


 

Iterative Search Algorithms

Iterative Search Algorithms for unique numbers that satisfy given conditions

– Chandrahas M. Halai

Find the unique four-digit number, where all the digits are not the same, such that when the number that you get by arranging the digits in ascending order is subtracted from the number that you get by arranging them in descending order you get the same four-digit number.

Let us choose any four-digit number and test whether it satisfies the given condition. Let the number be 1947 the year our country got its independence.

Arranging the digits in 1947 in descending order we get 9741 and arranging them in ascending order we get 1479. Now, subtracting 1479 from 9741 we get:

9741 – 1479 = 8262

Now, let us do the same operations on 8262 as the new input. We get:

8622 – 2268 = 6354

Now, let us do the above operations on every new output in iteration. We get:

6543 – 3456 = 3087

8730 – 0378 = 8352

8532 – 2358 = 6174

7641 – 1467 = 6174

Since, we are getting the same output for 6174, we have found the number we were looking out for.

Let us try the same procedure on one more four-digit number. Let that number be the current year 2021.

2210 – 0122 = 2088

8820 – 0288 = 8532

8532 – 2358 = 6174

7641 – 1467 = 6174

Again, we get the same number 6174.

If you do these operations on any four-digit number in which all the digits are not the same (a four-digit number consisting at least two different digits), then you will get the result as 6174 in not more than seven steps. This amazing discovery was made by Indian mathematician Shri Dattatreya Ramchandra Kaprekar (1905 – 1986) in the year 1949. In honour of Shri Kaprekar the number 6174 is called Kaprekar’s constant and the above procedure is called Kaprekar’s operations.

Let us define Kaprekar’s operations as a function k(x). The iterations in Kaprekar’s operations stop when k(a) = a. Here, we now know that a = 6174. That is k(6174) = 6174.

Let us say we begin Kaprekar’s operation with an initial four-digit number x1. The operations will stop when you reach the final value xn, where xn = 6174. Before reaching the final value xn, we get all the intermediate values, x2, x3, …, xn-1. All these values form what is known as the Kaprekar’s sequence. 6174, is the limiting value of these sequences.

From the above two examples we have the Kaprekar’s sequences as:

1) 1947, 8262, 6354, 3087, 8352, 6174

2) 2021, 2088, 8532, 6174

Let us generate a few more Kaprekar’s sequences:

3) 3355, 2178, 7443, 3996, 6264, 4176, 6174

4) 4434, 0999, 8991, 8082, 8532, 6174

5) 3579, 6174

6) 1357, 6174

7) 0246, 6174

8) 2468, 6174

Now, let us look at one more interesting number puzzle.

After a few hours of getting a new mobile phone number I forgot it. In India, we have ten digit mobile phone numbers. Fortunately, I had paid premium amount to get a special number. In this special number, the first digit (from left to right) is equal to number of zeroes in that number, the second digit is equal to the number of ones in that number, third digit is equal to the number of twos in that number and so on and so forth. For example, if first digit of number is 4 then there are four zeroes in that number. There is only one such ten-digit number. So, what is my mobile phone number?

The desired number is actually a self-descriptive number in base 10 (decimal number) number system.

A self-descriptive number is an integer m that in a given base b is b digits long in which each digit d at position n (the most significant digit being at position 0 and the least significant at position b−1) counts how many instances of digit n are in m.

Solution:

There are ten-billion ten-digit numbers from 0000000000 to 9999999999. Finding that one unique number amongst ten-billion is like finding a needle in a haystack.

I have developed an algorithm that constructs the number using the specified conditions.

We can begin solving the problem with a clean slate, i.e.

00000 00000

(I am breaking the number into two groups of five digits for better readability.)

Here we have ten zeroes. Hence, as per given conditions we should put a 10 in the first digit. Firstly 10 is not a digit and secondly putting any digit other than zero in the place of first digit will alter our count of zeroes in the number. The number of zeroes will be decreased by one, that is, now the number of zeroes will now be 9. Let us now put 9 in the first digit.

Hence, now our number will be

90000 00000

Our algorithm will work on this number to arrive at the required number. We begin by checking every digit in the number to ensure that it contains the right count of the corresponding digits. If not, then the right count is put in that particular place.

First pass or First Iteration:

(You will understand later why I am calling this step the First pass / iteration)

Checking the First digit:

This digit should have the count of zeroes in the number.

We see that it contains 9 and when we count the number of zeroes we see that it is also 9. Hence no change is made to the number. The number will be:

90000 00000

The digits from second to the ninth all contain zeroes and they are correct.

Checking the Tenth or the Last digit:

This digit should have the count of 9s in the number.

We see that there is one 9 in the number. Hence, we put a 1 in that place. Thus, our number becomes:

90000 00001

Have we got the desired ten-digit number? Let us check every digit of the number once again.

Second pass or Iteration:

    Checking the First digit:

At present this place contains a 9. Let us count the number of zeroes in the number. We see that there are eight zeroes instead of 9. Hence, we will replace the 9 with 8. Thus, the number becomes:

80000 00001

Checking the Second digit:

This digit should have the count of 1s in the number. At present it contains a 0. Let us count the number of 1s in the number. We see that there is one 1 (in the last digit). Hence, we will put a 1 in that place. Thus, the number becomes:

81000 00001

The digits from third to the Eighth all contain zeroes and they are correct.

Checking the Ninth digit:

This digit should have the count of 8s in the number. At present it contains a 0. Let us count the number of 8s in the number. We see that there is one 8 (in the first digit). Hence, we will put a 1 in that place. Thus, the number becomes:

81000 00011

Checking the Tenth or the Last digit:

This digit should have the count of 9s in the number.

At present that place contains a 1. Let us count the number of 9s in the number. We find that there are no 9s in the number. Hence, we put a 0 in that place. Thus, our number becomes:

81000 00010

Have we got the desired ten-digit number? Let us check every digit of the number once again. We are going to repeat the same steps (procedure) once again. We are going into a loop or iteration (iteration means repetition). When we find that all the digits contain the right count, we come out of the loop.

Third pass or Iteration:

    Checking the First digit:

At present this place contains an 8. Let us count the number of zeroes in the number. We see that there are seven zeroes instead of 8. Hence, we will replace the 8 with 7. Thus, the number becomes:

71000 00010

Checking the Second digit:

At present this place contains a 1. Let us count the number of 1s in the number. We see that there are two 1s instead of one. Hence, we will replace the 1 with 2. Thus, the number becomes:

72000 00010

Checking the Third digit:

At present it contains a 0. Let us count the number of 2s in the number. We see that there is one 2. Hence, we will put a 1 in that place. Thus, the number becomes:

72100 00010

The digits from fourth to the seventh all contain zeroes and they are correct.

Checking the Eighth digit:

At present it contains a 0. Let us count the number of 7s in the number. We see that there is one 7 (in the first digit). Hence, we will put a 1 in that place. Thus, the number becomes:

72100 00110

Checking the Ninth digit:

At present that place contains a 1. Let us count the number of 8s in the number. We find that there are no 8s in the number. Hence, we put a 0 in that place. Thus, our number becomes:

72100 00100

The last digit contains a zero and that is correct. Hence there will be no change in the number.

Have we got the desired ten-digit number? Let us check every digit of the number once again.

Fourth pass or Iteration:

    Checking the First digit:

At present this place contains a 7. Let us count the number of zeroes in the number. We see that there are six zeroes instead of 7. Hence, we will replace the 7 with 6. Thus, the number becomes:

62100 00100

The second and third digits contain a 2 and a 1 respectively and both are found to be correct.

The digits from fourth to the sixth all contain zeroes and they are correct.

Checking the Seventh digit:

At present this place contains a 0. Let us count the number of 6s in the number. We see that there is one 6 (in the first digit). Hence, we will put a 1 in that place. Thus, the number becomes:

62100 01100

Checking the Eighth digit:

At present that place contains a 1. Let us count the number of 7s in the number. We find that there are no 7s in the number. Hence, we put a 0 in that place. Thus, our number becomes:

62100 01000

The ninth and the last digits both contain a zero and that is correct. Hence there will be no change in the number.

Have we got the desired ten-digit number? Let us check every digit of the number once again.

Fifth pass or Iteration:

In this pass we find that all the digits are correct. No changes are made to the number in this pass. This means that we have got our desired mobile phone number.

Thus the mobile phone number is 62100 01000.

To get the mobile number we had applied the algorithm on the number 90000 00000. Instead of applying the algorithm on this, we can use any random ten-digit number and still get the correct answer.

I have written a program in Python to carry out the above operations on any ten-digit number. This can be found at:

https://chandrahasblogs.wordpress.com/2020/08/16/using-python-to-find-the-needle-in-the-haystack/

Let us try the above operations on another number, 9988776654. The results that we get after every iterations are as listed below:

Your 10 digit mobile number: 9988776654

[0, 0, 0, 0, 1, 1, 2, 0, 0, 0]

[7, 2, 2, 0, 0, 0, 0, 1, 0, 0]

[6, 1, 1, 0, 0, 0, 1, 0, 0, 0]

[6, 3, 0, 1, 0, 0, 1, 0, 0, 0]

[6, 2, 1, 0, 0, 0, 1, 0, 0, 0]

[6, 2, 1, 0, 0, 0, 1, 0, 0, 0]

Let me call these operations as the Chandrahas’ search operations. We can define these operations as a function c(x). The iterations in the search operations stop when c(a) = a. Here, we now know that a = 6210001000. That is c(6210001000) = 6210001000.

Let us say we begin the search operation with an initial ten-digit number x1. The operations will stop when you reach the final value xn, where xn = 6210001000. Before reaching the final value xn, we get all the intermediate values, x2, x3, …, xn-1. All these values form what is known as the Chandrahas’ search sequence. 6210001000, is the limiting value of these sequences.

From the above two examples we have the Chandrahas’ search sequences as:

1) 9000000000, 9000000001, 8100000010, 7210000100, 6210001000

2) 9988776654, 0000112000, 7220000100, 6110001000, 6301001000, 6210001000

Let us generate a few more sequences:

3) 0123456789, 1221111111, 0710000100, 7210000100, 6210001000

4) 5461532497, 0111100100, 5500020000, 7010000100, 7210000100, 6210001000

The algorithms / operations to find the solutions for both the above problems are similar. They are iterative search algorithms. You begin with any random number as the initial value and perform operations on it. The output is used as the new input. The operations are performed in iterations till the desired value is reached. The above algorithms can be grouped in to a class of Iterative Search Algorithms.

There will be similar mathematical problems which can be solved using iterative search algorithms.


Pingala’s Algorithms, Number of Meters

– Chandrahas M. Halai

This is in continuation of my earlier articles on Pingala’s Chandahshastra.

How Pingala created the Binary Number System

Pingala and Exponential

Till now we have dealt with poetic meters where all the four quarters of a verse had the same pattern of Laghus and Gurus. Such meters are called Samavrutta (समवृत्त) (even meters).

For a quarter having ‘n’ syllables we know that the total number of possible even meters is 2n.

What if all the four quarters of a verse are not the same?

If the first and the third quarters of a verse are similar and the second and the fourth quarters are similar, the meters are known as Ardhasamavrutta (अर्धसमवृत्त) (semi-even meters). In semi-even meters the halves have the same pattern.

If the numbers of syllables in all the four quarters are the same but the patterns are different it is called Visamavrutta (विषमवृत्त) (odd meters).

In Chandahshastra, Pingala has given sutras to calculate the sankhya (संख्या) total number of possible semi-even and odd meters for quarters having ‘n’ syllables each.

समं तावत्कृत्वः कृतमर्धसमम्। विषमं च​। राश्यूनम्।

(छन्द​: शास्त्रम् ५.३-५)

There are 2^n x 2^n possibilities in the first two quarters (as well as the third and the fourth). To get the number of semi-even meters we have to deduct the number of even-meters from it.

Therefore, the total number of possible semi-even meters for a quarter having ‘n’ syllables is (2^n)^2 – 2^n = 2^2n – 2^n

Now, let us take up the case of odd meters. For a verse having ‘n’ syllables in each quarter, we have 2^n x 2^n x 2^n x 2^n = (2^n)^4 = 2^4n possibilities. From this we have to deduct the number of even and semi-even meters.

2^4n – 2^n – (2^2n – 2^n) = 2^4n – 2^2n

Hence, the total number of possible odd meters for each quarters having ‘n’ syllables is 2^4n – 2^2n.

In the 11th century AD, Bhaskaracharya too had given the sutra to calculate the sankhyas of even, semi-even and odd meters in his book Lilavati.

पादाक्षरमितगच्छे गुणवर्गफलं चये द्विगुणे ।

समवृत्तानां संख्या तव्दर्गो वर्गवर्गश्च ॥

स्वस्वपदोनी स्यातां अर्धसमानं च् विषमाणाम् ॥१३९॥

Bhaskaracharya had also given a problem for finding the total number of possible even, semi-even and odd meters for Anustupa (अनुष्टुप) meter. Anustupa meter has 32 syllables in a verse and 8 syllables in each of the four quarters.

समानामर्धतुल्यानं विषमाणां पृथक् पृथक् ।

वृत्तानां वद मे संख्यामनुष्टुप्छंदसि द्रुतम् ॥१४०॥

Find the number of even, semi-even and odd meters in Anustupa meter?

Using the above formula, we get:

Number of even meters = 2^8 = 256

Number of semi-even meters = (2^8)^2 – 2^8 = 65,536 – 256 = 65280

Number of odd meters = (2^8)^4 – (2^8)^2 = 4294901760

Meru Prastaar (मेरु प्रस्तारः)

By Chandrahas M. Halai

प्रस्तारे मित्र गायत्र्याः स्युः पादे व्यक्तयः कति |

एकादिगुरवश्चाशु कथ्यतां तत्पृथक् पृथक् ||

O friend, there are six letters in the fourth line of Gayatri meter. If we choose only 1 Guru, 2 G, 3 G, 4 G, 5 G or 6 G, how many meters are possible?

This is a problem from Bhaskaracharya’s Lilavati.

How many meters with r Gurus (or Laghus) are possible in a prastaar for n syllables?

Pingala had developed a pratyay (method) to answer such questions.

Pingala’s sutra given in Chandahshastra for this method is:

परे पुर्णमिति |  – (छन्दः शास्त्रम् 8.34)

This is a very cryptic code. It was only through Halayudha Bhatt’s commentaries on Chandahshastra in his work Mritasanjeevani (composed in 10th century CE) that people were able to understand this sutra. Halayudha has explained how to create a table of numbers which he called मेरु प्रस्तार.

Table 1

Put one square in the first row. Refer Table 1. Then put 2 squares below it in the second row. Below this put 3 squares in the third row. In the fourth row put 4 squares and so on. If you want the combinations for a meter having n syllables then create a table having n + 1 rows.

Write 1 in the square in the first row. Then put 1s in the squares at the ends of each row. Refer Table 2.

Table 2

Numbers in the inner squares are got by adding the numbers in the two squares just above them. Refer Table 3. Let me explain this by calculating the number for the middle square of the third row. Both the squares above this square contain 1, hence we get 1 + 1 = 2. The number in this square gives number of meters having 1 Guru in a prastaar for 2 syllables.

Now let us calculate the value for the second square of the fourth row. The number in this square gives us the number of meters having 1 Guru in a prastaar for 3 syllables. The numbers in the two squares just above this square are 1 and 2. Hence, we get 1 + 2 = 3.

Now let us calculate the value for the third square of the fourth row. The number in this square gives us the number of meters having 2 Guru in a prastaar for 3 syllables. The numbers in the two squares just above this square are 2 and 1. Hence, we get 2 + 1 = 3.

Now let us calculate the value for the second square of the fifth row. The number in this square gives us the number of meters having 1 Guru in a prastaar for 4 syllables. The numbers in the two squares just above this square are 1 and 3. Hence, we get 1 + 3 = 4.

The seventh row of this table gives us the answer to problem posed at the beginning of this article. The first square of this row gives the number of meters when there are no Gurus. That is, all the six syllables are Laghus. This can only be done in 1 way and that is, LLLLLL.

The second square of the seventh row gives us the number of meters when there is only when one G in a quarter. This can be done in 6 different ways, viz:

1) G L L L L L

2) L G L L L L

3) L L G L L L

4) L L L G L L

5) L L L L G L

6) L L L L L G

The third square of the seventh row gives us the number of meters when there are 2 Gurus in a quarter. And so on.

Table 3

When we add up the numbers in all the squares of the seventh row it gives us the sankhya for the prastaar of 6 syllables.

S6 = 1 + 6 + 15 + 20 + 15 + 6 + 1 = 64

If we observe Table 3 closely, we can realise that the numbers in the squares are the binomial coefficients, nCr. Hence the number of metrical forms containing r Gurus (or Laghus) in a prastaar for n syllables is given by nCr.

Halayudha establishes that the basic rule for the construction of Meru Prastaar is the recurrence relation:

nCr = n-1Cr-1 + n-1Cr

Meru Prastaar’s form is like the steps to a hill. This must be the reason why Halayudha had called it Meru Prastaar. Meru is the name of a mountain.

Meru Prastaar is, what is popularly known as Pascal’s Triangle. But Pingala’s Meru Prastaar predates Pascal by at least 1800 years.

In the end I would like to present Bhaskaracharya’s method for calculating the binomial coefficients:

एकाधेकोत्तरा अंका व्यस्ता भाज्याः क्रमस्थितैः |

परः पूर्वेण संगुण्यस्तत्परस्तेन तेन च ||

एकद्वित्र्यादिभेदाः स्युरिदं साधारणं स्मृतम् |

छन्दश्चित्त्युत्तरे छन्दस्युपयोगोऽस्य तव्दिदाम् ॥
मूषावहनभेदादौ खण्डमेरौ च शिल्पके ।
वैध्यके रसभेदिये तन्नोक्तं विस्तृतेभर्यात् ॥

The number of combinations of n things taken r at a time are

[n (n – 1) (n – 2) … (n – (r – 1))] / [1 x 2 x 3 x … x r]

This is useful in prosody to discover all possible meters, in architecture, medical sciences, Khandmeru, chemical compositions etc. Bhaskaracharya used to call Meru prastaar as Khandmeru.

Pingala and Exponential

By Chandrahas M. Halai

या सृष्टिः स्रष्टुराद्या वहति विधिहुतं या हविर्या च होत्री
ये द्वे कालं विधत्तः श्रुतिविषयगुणा या स्थिता व्याप्य विश्वम् ।
यामाहुः सर्वबीजप्रकृतिरिति यया प्राणिनः प्राणवन्तः
प्रत्यक्षाभिः प्रपन्नस्तनुभिरवतु वस्ताभिरष्टाभिरीशः ॥ १॥

Meaning:

May the lord who is endowed with the eight visible forms protect you. The eight forms viz.

(1) That which is the first creation of the creator (water).

(2) That which conveys to the God, the oblation offered according to the customary rites (fire).

(3) That which is the sacrificer.

(4) and (5) Those two that regulate time (the sun and the moon).

(6) That which has sound for its quality and which pervades the universe (the ether).

(7) That which they call the source of all seeds (the earth).

(8) That by which the creatures possess breath (air).

This is the first verse of Kalidasa’s Abhijnanasakuntalam which is composed in the Sragdhara (स्रग्धरा) meter. Sragdhara has 21 syllables in each of its quarters. The pattern of Gurus and Laghus in Sragdhara is GGGGLGG LLLLLLG GLGGLGG.

Now, you may ask how many combinations of Gs and Ls are possible for a meter having 21 syllables? Or how many rows will be there in a prastaar for 21 syllables?

Refer to my earlier article How Pingala created the Binary Number system (part 1 of this series). The prastaar for 1 syllable has 2 rows. The prastaar for 2 syllables has 4 rows. The one for 3 syllables has 8 rows and the one for 4 syllables has 16 rows. And so on. The numbers of rows in a prastaar get doubled with every unit increment in the number of syllables. That is, number of rows in a prastaar of n-syllables is given by Sn = 2^n.

So the number of rows in a prastaar for a quarter having 21 syllables is 2^21. But what is the value of 2^21?

To calculate 2^21, I have to multiply 2 with itself 20 times. That is double the number 20 times. This will be a 20 step process. Is there a more efficient process? Yes!

Let us say I want to calculate a^21.

I can write

a^21 = (a^20) x a

= [(a^10)^2] x a

= [((a^5)^2)^2] x a

= [((a^4 x a)^2)^2] x a

= [(((a^2)^2 x a)^2)^2] x a

Now, this has become a 6 step process instead of a 20 step process.

Let us use this method to calculate S21 = 2^21.

S21 = 2^21 = [(((2^2)^2 x 2)^2)^2] x 2 = [((4^2 x 2)^2)^2] x 2 = [((16 x 2)^2)^2] x 2 = [(32^2)^2] x 2 = [(1024)^2] x 2 = 1048576 x 2 = 2097152

We can have 20,97,152 different meters having 21 syllables. That is there will be 20,97,152 rows in a prastaar for a quarter having 21 syllables.

You must be wondering who must have been the first person to have come up with the algorithm for quick exponential calculations.

Donald Knuth in his book The Art of Computer Programming, Volume 2, Seminumerical Algorithms, has mentioned that it was Pingala who was the first one to have developed such an algorithm in his work Chandahshastra. Pingala had developed an algorithm or pratyay (प्रत्याय) called Sankhya (संख्या) to quickly calculate the value of an exponential.

Pingala’s sutras for the Sankhya pratyay are:

द्विरर्धे | रुपे शून्यम् | द्विः शून्ये | तावदर्धे तद्गुणितम् |   (छन्दः शास्त्रम् 8.28-31)

  • If the number is divisible by 2, divide by 2 and write “2”.
  • If the number is not divisible by 2, deduct 1 and write “0”.
  • Proceed till you reach zero.
  • Start with the number 1 and scan the sequence of 2s and 0s from the end.
  • If you encounter “0”, multiply by 2.
  • If “2”, then square.

Let me illustrate this pratyay by calculating 2^21.

  • 21 is not divisible by 2, hence deduct 1 from 21 and write down “0”. We now have, n = 20 and the sequence as 0.
  • 20 is divisible by 2. Hence, n = n / 2 = 20 /2 = 10. Now, the sequence is 0, 2.
  • 10 is divisible by 2. Hence, n = n / 2 = 10 /2 = 5. Now, the sequence is 0, 2, 2.
  • 5 is not divisible by 2. Hence, n = n – 1 = 5 – 1= 4. Now, the sequence is 0, 2, 2, 0.
  • 4 is divisible by 2. Hence, n = n / 2 = 4 /2 = 2. Now, the sequence is 0, 2, 2, 0, 2.
  • 2 is divisible by 2. Hence, n = n / 2 = 2 /2 = 1. Now, the sequence is 0, 2, 2, 0, 2, 2.
  • 1 is not divisible by 2. Hence, n = n – 1 = 1 – 1= 0. Now, the sequence is 0, 2, 2, 0, 2, 2, 0.
  • We have reached “0”, hence we stop the process.
  • We start with the number 1 and scan the sequence from the end.
  • We first encounter a 0, hence we have, 1 x 2 = 2.
  • Then we encounter a 2, hence we have, 2^2 = 4.
  • Again we encounter a 2, hence we have, 4^2 = 16.
  • Then we have 0, hence we have, 16 x 2 = 32.
  • We now have a 2, hence we square the number, 32^2 = 1024.
  • We again have a 2, hence we have, (1024)^2 = 1048576
  • And last we have a 0, hence we have, 1048576 x 2 = 20,97,152.

S21 = 20,97,152

Let us look at one more example, say S15 = 2^15.

We have n = 15.

  • n = n – 1 = 15 – 1 = 14. Sequence = 0.
  • n = n / 2 = 14/2 = 7. Sequence = 0, 2.
  • n = n – 1 = 7 – 1 = 6. Sequence = 0, 2, 0.
  • n = n / 2 = 6/2 = 3. Sequence = 0, 2, 0, 2.
  • n = n – 1 = 3 – 1 = 2. Sequence = 0, 2, 0, 2, 0.
  • n = n / 2 = 2/2 = 1. Sequence = 0, 2, 0, 2, 0, 2.
  • n = n – 1 = 1 – 1 = 0. Sequence = 0, 2, 0, 2, 0, 2, 0.

So the final sequence is 0, 2, 0, 2, 0, 2, 0.

  • 1 x 2 = 2
  • 2^2 = 4
  • 4 x 2 = 8
  • 8^2 = 64
  • 64 x 2 = 128
  • 128^2 = 16384
  • 16384 x 2 = 32768

Hence we S15 = 32768

Two more sutras are associated with Pingala’s Sankhya pratyay:

द्विर्द्यूनं तदन्तानाम् |   (छन्दः शास्त्रम् 8.32)

Meaning: the sum of all sankhyas Sr for r = 1, 2, 3, …, n is

S1 + S2 + S3 + … + Sn = 2Sn – 2

परे पूर्णम् |   (छन्दः शास्त्रम् 8.33)

Meaning:

Sn+1 = 2Sn

Pingala’s Sankhya was one of the earliest and quickest methods to calculate the exponential.

Pingala’s Algorithm to find the value of a Binary Sequence – Part IV

By Chandrahas M. Halai

In my earlier article “Pingala’s Algorithm for Binary Conversion”, I had described how given a row number of an n-syllable prastaar you can find the sequence of Gurus and Laghus in that row using the Nashtam pratyay of Pingala. Now, what if given a sequence of n Gurus and Laghus we want to know its row number? This is essentially a converse procedure of Nashtam.

To understand how this was achieved, first let us closely observe the Nashtam pratyay. Let me present the sutras for the Nashtam pratyay:

लर्धे | सैके ग् | (छन्दः शास्त्रम् 8.24-25)

  • To find the pattern in a row of the prastaar, start with the row number.
  • Halve it (if possible) and write an L.
  • If it cannot be halved, add one and halve and write a G.
  • Proceed till all the syllables of the metre are found.

Let us use these to find the sequence of Gs and Ls in the 28th row of a prastaar of 8 syllables.

We have, n = 28.

Step 1) n = 28. It is divisible by 2. Therefore the first letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 28/2 = 14

Step 2) n = 14. It is divisible by 2. Therefore the second letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 14/2 = 7

Step 3) n = 7. It is not divisible by 2. Therefore the third letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 7 + 1 = 8

n = n/2 = 8/2 = 4

Step 4) n = 4. It is divisible by 2. Therefore the fourth letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 4/2 = 2

Step 5) n = 2. It is divisible by 2. Therefore the fifth letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 2/2 = 1

Step 6) n = 1. It is not divisible by 2. Therefore the sixth letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 1 + 1 = 2

n = n/2 = 2/2 = 1

Step 7) n = 1. It is not divisible by 2. Therefore the seventh letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 1 + 1 = 2

n = n/2 = 2/2 = 1

Step 8) n = 1. It is not divisible by 2. Therefore the eighth letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 1 + 1 = 2

n = n/2 = 2/2 = 1

Since we have got the required 8 syllables, we stop the procedure.

Hence, the pattern for the 28th row is LLGLLGGG.

If we observe the steps 6, 7 and 8 closely we realise that once the quotient becomes 1 all the trailing syllables are Gurus. Pingala has made good use of this to reduce the number of steps in his converse pratyay, Uddishta.

Now, given a sequence of n Gurus and Laghus can we find the correct row number in the n-syllables prastaar. Pingala had developed an algorithm (pratyay / प्रत्याय) called Uddishta (उद्दिष्ट) to achieve this.

Pingala’s sutras for Uddishta :

प्रतिलोमगणं द्विर्लाद्यम् |  ततोग्येकं जह्यात् | (छन्दः शास्त्रम् 8.26-27)

Meaning:

  • Start with the number 1.
  • Scan the sequence in reverse order (that is from the right) and reach the first laghu. Now double the number.
  • Double the number when an L is encountered.
  • Double and reduce by 1 when a G is encountered.

Let me illustrate this by getting the row number for the sequence LLGLLGGG.

  • Start with the number 1. Notice that in Nashtam we ended the process with a 1.
  • Start scanning the sequence from the right. Skip the Gs and reach the first L. (Recollect our earlier observation in the Nashtam procedure when we encountered trailing Gs towards the right of the sequence.) Now, double the number. We get, 1 x 2 = 2.
  • Moving to next syllable in the left we encounter an L. Hence, double the number. We get, 2 x 2 = 4.
  • Moving to next syllable in the left we encounter a G. Hence, double the number and deduct 1. We get, (4 x 2) – 1 = 8 – 1 = 7. Recollect that in Nashtam, when we encounter a G, we add a 1 and then halve it.
  • Moving to next syllable in the left we encounter an L. Hence, double the number. We get, 7 x 2 = 14.
  • Moving to next syllable in the left we encounter an L. Hence, double the number. We get, 14 x 2 = 28.

Hence the row number in an 8-syllables prastaar for the sequence LLGLLGGG is 28.

We also know that the row number in a prastaar is value of the binary sequence plus one.

Replacing G and L in LLGLLGGG with 0 and 1 we get 11011000, this is mirror image of the binary number. Hence, the binary number is 00011011. The value of this binary number as calculated by the modern method is:

(00011011)2 = (1 x 2^4) + (1 x 2^3) + (0 x 2^2) + (1 x 2^1) + (1 x 2^0) = 16 + 8 + 2 + 1 = 27

Therefore the row number = 27 + 1 = 28.

Kedar Bhatt was the first to give such an algorithm in his work Vruttaratnakar. Kedar Bhatt’s sutras for Uddishta are:

उद्दिष्टं द्विगुणानाद्यादुपर्यङ्कान् समालिखेत् |

लघुस्था ये तु तत्राङ्कास्तैः सैकैर्मिश्रितैर्भवेत् ||        (वृत्तरत्नाकरः  6.5)  

Meaning:

  • Place 1 on top of the left-most syllable of the given sequence.
  • Double it at each position while moving to the right.
  • Sum the numbers above L and add 1 to get the row-number.

Let us apply this algorithm to get the row number for the above sequence, i.e. LLGLLGGG. Refer Table 1.

Table 1

1248163264128
LLGLLGGG

Row number = 1 + 2 + 8 + 16 + 1 = 28

Essentially Kedar Bhatt is calculating the powers of 2. Refer Table 2.

Table 2

2^02^12^22^32^42^52^62^7
1248163264128
LLGLLGGG

Even after 1000 years we are using the same method as that of Kedar Bhatt to get the value of binary sequences. When we compare the Uddishta pratyay’s of Pingala and Kedar Bhatt we find that Pingala’s method is more efficient.

Prastaar of Kedar Bhatt

By Chandrahas M. Halai

This is in continuation of my two previous articles – How Pingala created the Binary Number System and Pingala’s Algorithm for Binary Conversion.

In the 11th century CE, Kedar Bhatt had composed his work on the non-Vedic meters named Vruttaratnakar (वृत्तरत्नाकरः). This book contains six chapters, of which the last chapter is devoted to algorithms related to combinatorial mathematics. Though these algorithms accomplish essentially the same things as the algorithms (pratyay) developed by Pingala, but their approach is different.

Pingala’s algorithms are recursive, meaning to write down the prastaar of a meter containing 6 syllables one needs to have a prastaar for 5 syllables. And to write a prastaar for 5 syllables you need to have a prastaar for 4 syllables and so on. Whereas in the algorithm developed by Kedar Bhatt to write down the prastaar for a meter having 6 syllables I don’t need to have a prastaar for 5 syllables. So, let us have a look at Kedar Bhatt’s prastaar.

The sutras for prastaar as given by Kedar Bhatt are:

पादे सर्वगुरावाद्याल्लघुं न्यस्य गुरोरधः |

यथोपरि तथा शेषं भूयः कुर्यादमुं विधिम् ||   (वृत्तरत्नाकरः  6.2)

ऊने दध्याद् गुरूनेव यावत्सर्वलघुर्भवेत् |

प्रस्तारोऽयं समाख्यातश्छन्दोविचितिवेदिभिः ||    (वृत्तरत्नाकरः  6.3)

Meaning:

Begin with a row containing all gurus (G). (For a prastaar for 4 syllables, begin with a row containing all 4 gurus). Start scanning the row from the left. When you encounter the first guru, place a laghu (L) below that. The syllables to the right are copied below as they are. All the places to the left of L are filled up with Gs. Repeat the process for the subsequent rows till you reach the row having all Ls.

I will illustrate Kedar Bhatt’s algorithm by writing down a prastaar for a meter containing 4 syllables.

Table 1

1GGGGStart with a row containing all Gs.
2LGGGPlace L below the first G of above row. The syllables to the right are copied as they are.
3GLGGAll the places to the left of L are filled up with Gs.
4LLGGPlace L below the first G of above row. The syllables to the right are copied as they are.
5GGLG 
6LGLG 
7GLLG 
8LLLG 
9GGGL 
10LGGL 
11GLGL 
12LLGL 
13GGLL 
14LGLL 
15GLLL 
16LLLLThe process stops here.

Compare this table with Table 5 in the article “How Pingala created the Binary Number System”.

You must be wondering how Kedar Bhatt come up with such an algorithm?

The answer is Binary Addition.

So, let’s understand the basics of binary addition.

Let me add a 1 to 0. We have,

              0

            +1

__________

              1

Now, let us add a 1 to 1.

              1

            +1

__________

             10

The binary number system like the decimal number system is a place-value system. In the decimal number system you have ten digits (0 and 1 through 9). Likewise in the binary number system you have only two digits, 0 and 1, which are called bits. Bit is short for Binary digit. The binary number system is a base two number system. In decimal number system the value of the digit depends on its position, i.e. digit times the power of ten. In the binary number system too, the value of the digit depends on its position, but here it is digit times the power of 2.

When you add a 1 to 1, the result is 2, which in binary is 10. Hence, write a 0 in the current column and carry over a 1 to the next column.

Now, let us add a 1 to 10. We have,

              10

              +1

          _____

              11

11 is 3 in decimal.

Now, let us add a 1 to 11. We have,

              11

              +1

          _____

            100

100 is 4 in decimal. Here, when we add a 1 to the 1 in the rightmost column we get a 10. Hence, a 1 is carried forward to the next higher column. The next column already contains a 1. So, the same process is repeated.

Now, let us add a 1 to 0111. We have,

0111
+  1
1000

If we pay attention to this last addition, we can see similarities between Kedar Bhatt’s algorithm and binary addition. In this addition, we scan the number 0111 from the right and set the first zero we encounter to 1. And fill up all the bits to its right to 0s.

To make it clear let me give one more example. Let us add 1 to 10100111 (which is 167 in value / decimal).

10100111
+      1
10101000

In this addition, we scan the number 10100111 from the right and set the first zero we encounter to 1. We fill up all the bits to its right to 0s. The bits to the left are copied below as it is.

If we replace Gs and Ls in Table 1 with 0s and 1s, we get the binary sequence for numbers from 0 to 15 in reverse order. Hence, it must have become clear to all that Kedar Bhatt must have developed his Prastaar algorithm from the principles of binary addition.

During ancient times, the prastaar used to be written on the sand. There was a possibility that the wind might blow away the sands and erase some rows of the prastaar. So, how do you restore an erased / damaged row?

In my previous article “Pingala’s Algorithm for Binary Conversion”, I had discussed Pingala’s technique (pratyay, प्रत्याय) or algorithm called Nashtam (नष्टम्) to regenerate the sequence of Guru and Laghu syllables of a row with a given row / index number.

To restore an erased row, take a previous intact row and apply Kedar Bhatt’s procedure to it. Let me illustrate this by applying it to find the 29th row of a prastaar for a quarter of length 5. The guru and laghu sequence for the 28th row is LLGLL.

LLGLLGuru and Laghu sequence for the 28th row
GGLLLScan the previous row from the left. When you encounter the first G, put a L below it. The syllables to the right are copied as they are. All the places to the left of L are filled up with Gs.

Hence, the guru and laghu sequence for the 29th row is GGLLL.

From the above discussion it is obvious that Kedar Bhatt had good understanding of Binary Addition.

Using Python to find the Needle in the Haystack

This is in continuation of my earlier article:

https://chandrahasblogs.wordpress.com/2019/12/08/finding-a-needle-in-a-haystack/

Given below is the Python code to solve the given problem.

def prem_num():

lst = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

mob_num = input(“Your 10 digit mobile number: “)

dgt = 0

while (dgt < 10):

lst[dgt] = int(mob_num[dgt])

dgt += 1

stp = 1

while (stp <= 10):

noChange = True

dgt = 0

while (dgt < 10):

cnt = 0

pos = 0

while (pos < 10):

if (dgt == lst[pos]):

cnt += 1

pos += 1

if (lst[dgt] != cnt):

lst[dgt] = cnt

noChange = False

dgt += 1

print(lst)

if(noChange):

break

stp += 1

prem_num()

Pingala’s Algorithm for Binary Conversion

By Chandrahas M. Halai

This is in continuation of my earlier article.

https://chandrahasblogs.wordpress.com/2020/04/22/how-pingala-created-the-binary-number-system/

Today’s digital technologies work on binary number system. All your data is encoded in binary numbers, 0 and 1. For example, the number 235 is 1110 1011 in binary (base-two number system). But how do you convert a decimal number into its binary equivalent? Let us look at it in this article.

We generally use the decimal place-value number system. In this system, we have

235 = (2 * 100) + (3 * 10) + (5 * 1) = (2 * 10^2) + (3 * 10^1) + (5 * 10^0)

Now, we can use any number, instead of just 10, as our base for place-value number system.

We can use a polynomial in base number to represent a number in a place-value number system. Let x, be any base number. Then, we have

anxn + an-1xn-1 + an-2xn-2 + … + a2x2 + a1x1 + a0x0 = (anan-1an-2…a2a1a0)x

Here, an, an-1, an-2, …, a2, a1, a0 are the digits in the base x number system. They can take on values from 0 to (x – 1).

Now, let us represent the decimal number 235 in some base  number system.

We have

… + anxn + an-1xn-1 + an-2xn-2 + … + a2x2 + a1x1 + a0x0 = (235)10         – eqn (1)

Here, we don’t know how many digits in base  number system are required to represent (235)10.

Now, how do we find the digits in the base x number system?

What if we divide both the sides of eqn (1) with x?

We get, a0 as the remainder on the left side of eqn (1). Similarly we also get a remainder on the right side of eqn (1). This remainder will be the digit in the lowest value place.

We also get … + anxn-1 + an-1xn-2 + an-2xn-3 + … + a2x1 + a1 as the quotient. This will be our new dividend for getting the digit in the second lowest value place. Let us divide it by x.

We get, a1 as the remainder. This remainder will be the digit in the second lowest value place.

We also get … + anxn-2 + an-1xn-3 + an-2xn-4 + … + a3x + a2 as the quotient. This will be our new dividend for getting the digit in the third lowest value place. Let us divide it by x.

We get, a2 as the remainder. This remainder will be the digit in the third lowest value place.

We also get … + anxn-3 + an-1xn-4 + an-2xn-5 + … + a3 as the quotient. This will be our new dividend for getting the digit in the next higher value place.

To get the digits in the higher value places go on repeating the above procedure. The process stops when the quotient becomes zero.

Thus, we get the representation of (235)10 in base x number system as

(… anan-1an-2 … a2a1a0)x.

As an example, let us convert the decimal number 235 into binary.

Since, we want to convert a decimal number 235 into binary we will divide it by 2.

Step 1) 235/2

Quotient = 117, remainder = 1

Thus, 1 will be the digit in the lowest value place.

Step 2) 117/2

Quotient = 58, remainder = 1

Step 3) 58/2

Quotient = 29, remainder = 0

Step 4) 29/2

Quotient = 14, remainder = 1

Step 5) 14/2

Quotient = 7, remainder = 0

Step 6) 7/2

Quotient = 3, remainder = 1

Step 7) 3/2

Quotient = 1, remainder = 1

Step 8) 1/2

Quotient = 0, remainder = 1

Since here, the quotient is 0, the division process comes to an end.

Thus, the binary representation of the decimal number 235 is 1110 1011.

Pingala was the first person to have done such a binary conversion. Let us look at how he must have developed such an algorithm.

In my previous article I had described how Pingala had developed technique (pratyay, प्रत्यय) or algorithm called Prastaar (प्रस्तार , meaning to unfold or to open up) for enlisting all the possible combinations of Guru (G) and Laghu (L) syllables for a quarter with length n letters.

Let me present here a prastaar for a quarter with length 5 letters. Refer Table 1.

Row/Index Number Sequences of G and L
1 GGGGG
2 LGGGG
3 GLGGG
4 LLGGG
5 GGLGG
6 LGLGG
7 GLLGG
8 LLLGG
9 GGGLG
10 LGGLG
11 GLGLG
12 LLGLG
13 GGLLG
14 LGLLG
15 GLLLG
16 LLLLG
17 GGGGL
18 LGGGL
19 GLGGL
20 LLGGL
21 GGLGL
22 LGLGL
23 GLLGL
24 LLLGL
25 GGGLL
26 LGGLL
27 GLGLL
28 LLGLL
29 GGLLL
30 LGLLL
31 GLLLL
32 LLLLL

Table 1

During ancient times, the prastaar used to be written on the sand. There was a possibility that the wind might blow away the sands and erase some rows of the prastaar. So, how do you restore an erased / damaged row?

Pingala had developed a technique (pratyay, प्रत्यय) or algorithm called Nashtam (नष्टम्) to regenerate the sequence of Guru and Laghu syllables of a row with a given row / index number.

If we replace G with ‘0’ and L with ‘1’ in Table 1, we get binary number system developed by Pingala. Refer Table 2.

Row/Index Number Sequences of G and L Binary Sequence generated by Pingala Binary Numbers
1 GGGGG 00000 00000
2 LGGGG 10000 00001
3 GLGGG 01000 00010
4 LLGGG 11000 00011
5 GGLGG 00100 00100
6 LGLGG 10100 00101
7 GLLGG 01100 00110
8 LLLGG 11100 00111
9 GGGLG 00010 01000
10 LGGLG 10010 01001
11 GLGLG 01010 01010
12 LLGLG 11010 01011
13 GGLLG 00110 01100
14 LGLLG 10110 01101
15 GLLLG 01110 01110
16 LLLLG 11110 01111
17 GGGGL 00001 10000
18 LGGGL 10001 10001
19 GLGGL 01001 10010
20 LLGGL 11001 10011
21 GGLGL 00101 10100
22 LGLGL 10101 10101
23 GLLGL 01101 10110
24 LLLGL 11101 10111
25 GGGLL 00011 11000
26 LGGLL 10011 11001
27 GLGLL 01011 11010
28 LLGLL 11011 11011
29 GGLLL 00111 11100
30 LGLLL 10111 11101
31 GLLLL 01111 11110
32 LLLLL 11111 11111

Table 2

I would now like to draw your attention to two things in Table 2.

1) In the binary sequence generated by Pingala, the numbers are written with the higher place value digits to the right of lower place value digits. As can be seen from Table 2, the binary sequences generated by Pingala are mirror images of the modern representation of binary numbers.

2) The counting of row / index numbers begins from 1. It means that the row number is one more than the value of the binary number.

Pingala’s rule for binary conversion

लर्धे | सैके ग् | (छन्दः शास्त्रम् 8.24-25)

  • To find the pattern in a row of the prastaar, start with the row number.
  • Halve it (if possible) and write an L.
  • If it cannot be halved, add one and halve and write a G.
  • Proceed till all the syllables of the metre are found.

Let us now find the nth row in a prastaar for m characters in a quarter.

We have

n = value of the number + 1

Let bm-1bm-2…b2b1b0 represent the binary number having m bits. We can write this binary number as a polynomial in 2 as

(bm-1 * 2m-1) + (bm-2 * 2m-2) + … + (b2 * 22) + (b1 * 21) + b0

Now, we have

(bm-1 * 2m-1) + (bm-2 * 2m-2) + … + (b2 * 22) + (b1 * 21) + b0 + 1 = n        – eqn (2)

In eqn (2), b0 is the bit in the lowest value place. To get the value of b0 we should divide both the sides of eqn (2) by 2. In this two scenarios can occur:

1) The number n is divisible by 2

It means that, b0 + 1 = 2, then b0 = 1.

This means that the first letter in the nth row is a Laghu (L) syllable.

This is what was precisely stated as a rule by Pingala. Pingala’s first sutra for binary conversion states that if the number n can be halved, write a Laghu (L) and halve the number.

2) The number n is not divisible by 2

It means that, b0 + 1 = 1, then b0 = 0.

This means that the first letter in the nth row is a Guru (G) syllable.

Pingala’s second sutra for binary conversion states that if the number n cannot be halved, write a Guru (G), then add a 1 to the number n and halve it.

Let us add 1 to both sides of eqn (2):

(bm-1 * 2m-1) + (bm-2 * 2m-2) + … + (b2 * 22) + (b1 * 21) + b0 + 1 + 1 = n + 1

– eqn (3)

The quotient of this division will be the dividend to find the next high value place digit, b1.

The same procedure is repeated to find the remaining binary digits of the row. The division stops when we get all the m digits of the row.

Let me illustrate the above procedure by applying it to find the 22nd row of a prastaar for quarter of length 5 (Table 2).

We have, n = 22.

Step 1) n = 22. It is divisible by 2. Therefore the first letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 22/2 = 11

Step 2) n = 11. It is not divisible by 2. Therefore the second letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 11 + 1 = 12

n = n/2 = 12/2 = 6

Step 3) n = 6. It is divisible by 2. Therefore the third letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 6/2 = 3

Step 4) n = 3. It is not divisible by 2. Therefore the fourth letter in the row is a Guru (G).

Now, add a 1 to the number and then halve it.

n = n + 1 = 3 + 1 = 4

n = n/2 = 4/2 = 2

Step 5) n = 2. It is divisible by 2. Therefore the fifth letter in the row is a Laghu (L).

Now, halve the number:

n = n/2 = 2/2 = 1

Thus, the arrangement of Guru and Laghu syllables in the 22nd row of a prastaar for quarter of length 5 is LGLGL. Checking this in Table 2, we find it to be correct.

From the above discussion we can infer that:

  1. Pingala and Indian mathematicians of his time had the knowledge of place-value number systems.
  2. They had the knowledge of long division of polynomials.
  3. Pingala understood and had developed the algorithm for radix conversion.

 

How Pingala created the Binary Number System

By Chandrahas M. Halai

“Physically separated but digitally connected!” should be our motto in these days of the Corona virus pandemic. Truly speaking digital technologies have enabled us to get through these days of lockdown.

Do you know, what is the fundamental principle on which today’s digital technology works?

It is the binary number system.

But, do you know that this is the gift of ancient Indian mathematics to the world of technology. Ancient Indian prosodist and mathematician Pingala was the first to develop and use the binary number system while studying and analysing Sanskrit poetic meters (chandah / छन्द:) in his work Chandahshastra composed around 3rd century BCE.

The binary number system like the decimal number system is a place-value system. In the decimal number system you have ten digits (0 and 1 though 9). Likewise in the binary number system you have only two digits, 0 and 1, which are called bits. Bit is short for Binary digit. The binary number system is a base two number system. In decimal number system the value of the digit depends on its position, i.e. digit times the power of ten. In the binary number system too, the value of the digit depends on its position, but here it is digit times the power of 2.

For example, the decimal number 237 is

237 = (2 x 100) + (3 x 10) + (7 x 1) = (2 x 10^2) + (3 x 10^1) + (7 x 10^0)

Binary equivalent of 237 is (1110 1101)2.

(1110 1101)2 = (1 x 2^7) + (1 x 2^6) + (1 x 2^5) + (0 x 2^4) + (1 x 2^3) + (1 x 2^2) + (0 x 2^1) + (1 x 2^0) = 128 + 64 + 32 + 0 + 8 + 4 + 0 + 1 = 237

Table 1 gives you the binary equivalent of decimal numbers from 0 to 31. Kindly observe the pattern of the bits of the binary numbers in this table.

Decimal Binary Decimal Binary
0 0 0000 16 1 0000
1 0 0001 17 1 0001
2 0 0010 18 1 0010
3 0 0011 19 1 0011
4 0 0100 20 1 0100
5 0 0101 21 1 0101
6 0 0110 22 1 0110
7 0 0111 23 1 0111
8 0 1000 24 1 1000
9 0 1001 25 1 1001
10 0 1010 26 1 1010
11 0 1011 27 1 1011
12 0 1100 28 1 1100
13 0 1101 29 1 1101
14 0 1110 30 1 1110
15 0 1111 31 1 1111

Table 1

You must now be wondering how Pingala developed binary number system? To understand this let us first understand the basics of Sanskrit prosody.

Letters in Sanskrit can be either long (Guru / गुरु स्वर) or short (Laghu / लघु स्वर) syllables. These two types of syllables are the basic building blocks of Sanskrit prosody. A verse in classical Sanskrit literature has four quarters or pada (पाद) in a stanza. The metre of a verse is determined by the number of syllables and their order of arrangement in a quarter.

A syllable (अक्षर) is a vowel or a vowel with one or more consonants preceding it.

A short syllable is one ending with one of the short (hrasva / ह्रस्व ) vowels, which are a (अ), i (इ), u (उ), ṛ (ऋ) and ḷ (ऌ).

Even a short syllable will be a guru if what follows is a conjunct consonant, an anusvara or a visarga.

The long syllable is defined as one with one of the long (dirgha / दीर्घ) vowels, which are ā (आ), ī (ई), ū (ऊ), ṝ (ॠ), e (ए), ai (ऐ), o (ओ) and au (औ).

The last syllable of a foot of a metre is taken to be guru optionally.

 

Let us now understand Sanskrit metre with an example. Given below are the first two of a total of eight stanzas of Bhavani ashtakam (भवान्यष्टकम्), a beautiful composition by Adi Shankaracharya.

भवान्यष्टकम्

न तातो न माता न बन्धुर्न दाता

न पुत्रो न पुत्री न भृत्यो न भर्ता।

न जाया न विद्या न वृत्तिर्ममैव

गतिस्त्वं गतिस्त्वं त्वमेका भवानि ॥१॥

Neither father, nor mother; neither the relative, nor the benefactor,
Neither son, nor daughter; neither servant, nor husband,
Neither wife, nor the (worldly) knowledge; nor Profession,
You alone are my refuge, you alone are my refuge, Oh Mother Bhavani.

भवाब्धावपारे महादुःखभीरु:

पपात प्रकामी प्रलोभी प्रमत्तः।

कुसंसारपाशप्रबद्धः सदाहं

गतिस्त्वं गतिस्त्वं त्वमेका भवानि ॥२॥

In the endless ocean of worldly existence, I am full of sorrow and much afraid,

I have fallen with excessive desires and greed, drunken and intoxicated,

Always tied in the bondage of this miserable samsara (worldly existence),

You alone are my refuge, you alone are my refuge, Oh Mother Bhavani.

 

Every quarter of Bhavani ashtakam consists of 12 letters. The pattern or the order of arrangement of syllables in each quarter is

LGG LGG LGG LGG

Here, G is Guru and L is Laghu syllable.

This metre is called भुजङ्गप्रयातम्  (Bhujangaprayatam, snake-like advancing).

For a quarter having 12 letters, one can have 2^12 = 4096 different arrangements of Guru and Laghu syllables. This means that you can have 4096 different metres with a quarter consisting of 12 letters.

Pingala had developed technique (pratyay, प्रत्यय) or algorithm called Prastaar (प्रस्तार , meaning to unfold or to open up) for enlisting all the possible combinations of syllables for a quarter with length n letters.

For a metre having only one syllable, Pingala developed the following matrix / table:

1 G
2 L

Table 2

 

For a metre having 2 syllables:

In step 1, Pingala copied the previous matrix immediately below itself. And in step 2, he filled up the upper half of the next column with 2 Gs and lower half with 2 Ls as shown in Table 3.

1 G G
2 L G
3 G L
4 L L

Table 3

 

For a metre having 3 syllables:

In step 1, Pingala copied the previous matrix immediately below itself. And in step 2, he filled up the upper half of the last column with 4 Gs and lower half with 4 Ls as shown in Table 4.

1 G G G
2 L G G
3 G L G
4 L L G
5 G G L
6 L G L
7 G L L
8 L L L

Table 4

 

For a metre having 4 syllables:

In step 1, Pingala copied the previous matrix immediately below itself. And in step 2, he filled up the upper half of the last column with 8 Gs and lower half with 8 Ls as shown in Table 5.

1 G G G G
2 L G G G
3 G L G G
4 L L G G
5 G G L G
6 L G L G
7 G L L G
8 L L L G
9 G G G L
10 L G G L
11 G L G L
12 L L G L
13 G G L L
14 L G L L
15 G L L L
16 L L L L

Table 5

 

For metres with larger number of letters we can repeat this technique recursively.

If we now replace G with 0 and L with 1 in Table 5, we get:

1 0 0 0 0
2 1 0 0 0
3 0 1 0 0
4 1 1 0 0
5 0 0 1 0
6 1 0 1 0
7 0 1 1 0
8 1 1 1 0
9 0 0 0 1
10 1 0 0 1
11 0 1 0 1
12 1 1 0 1
13 0 0 1 1
14 1 0 1 1
15 0 1 1 1
16 1 1 1 1

Table 6

 

If you compare Table 6 with Table 1 you will observe that the entries in Table 6 are mirror images of the binary equivalents of the numbers from 0 to 15. Refer Table 7.

Decimal Number Binary Sequence generated by Pingala Binary Number
0 0000 0000
1 1000 0001
2 0100 0010
3 1100 0011
4 0010 0100
5 1010 0101
6 0110 0110
7 1110 0111
8 0001 1000
9 1001 1001
10 0101 1010
11 1101 1011
12 0011 1100
13 1011 1101
14 0111 1110
15 1111 1111

Table 7

This is how Pingala created the binary number system. This is one of the most significant contributions of India to the world of Mathematics.