Methods for factoring numbers, in the Gaṇita-kaumudī of Nārāyaṇa Paṇḍita
The Gaṇita-kaumudī (गणितकौमुदी) is a work on mathematics written by Nārāyaṇa Paṇḍita in 1356 CE. Also known as the Gaṇita-pāṭī-kaumudī, it contains 14 chapters (see Wikipedia article), of which chapters 13 (on combinatorics) and 14 (on magic squares) are especially advanced/notable.
Recently I took a close look at Chapter 11, which is about factoring numbers. (I had taken a quick look earlier, as among other things, it contains a description of what is now called “Fermat’s factorization method”, named after Pierre de Fermat who independently rediscovered the method about 300 years later, in the 17th century.) I wanted to take a look at the Sanskrit text myself, and try my hand at understanding/translating each verse, before turning to the published commentary and translation when necessary. It is a short chapter, of 19 verses in the āryā metre.
Here they are. I didn’t bother to transcribe the Sanskrit text from images. (Actually, this post exists thanks to Logseq’s PDF annotation feature that creates an image of a region of a page, as I took notes while reading the chapter.) To prevent the images from shrinking too much, I’ve collapsed the two notes columns by default; click on the little triangles to expand and read, and click again to collapse.
Original text | My translation | Notes on the mathematics | Notes on the translation |
---|---|---|---|
|
|||
![]() |
End of chapter 10 | ||
![]() |
Start of chapter 11: factoring | ||
![]() |
I shall tell you of the delight of mathematicians, the joy of factoring. Learning this turns anyone into a mathematician. | ||
![]() |
Repeatedly divide an even number by 2, until it becomes odd. If it (the number to be factored) has 5 in the first [units] place, divide by 5. |
|
|
![]() |
When |
|
|
![]() |
While you find divisors, keep finding divisors. | hara = divisor |
|
![]() |
If |
[An alternative method starts here…] |
|
![]() |
When |
That is, there exists some square |
|
![]() |
Of these numbers, as before, find factors. | That is, factorize the |
rāśi = quantity, number — a number that has been written down, e.g. an output of a previous step |
![]() |
Of a non-square number, find the nearest (integer) square root, double it and add 1… | ||
![]() |
…and subtract from it the square-remainder. If the result is a square, then this is the number to be added. | That is, let |
|
![]() |
If it is not a square, then further add |
The idea is that we’re trying successive values for the smaller square: if |
|
![]() |
…and so on in arithmetic progression, until you get a square. | So to |
I think uttara = difference and uttaravṛddhi = arithmetic progression. |
![]() |
For unequal divisors, put the previous products in front of them, and again separately. For equal divisors, put their products with each of the previous products, and again separately. | This is a procedure to list all divisors of a number.
|
Took me a while to understand but I think the anvaya is:
|
![]() ![]() |
Quickly find the divisors of 2048 and 3125. | Examples (exercises). Solutions:
|
|
![]() |
Find the divisors of 7250, and those of 10201. | Solutions: It turns out that |
|
![]() |
List the divisors of 1161, if you’re a mathematician who knows how to factor. | Solutions: It turns out that |
candra-aṅga-bhū-bhuvaḥ = 1-6-1-1 = 1161. |
![]() |
The divisors of 1001 — name them quickly, if you have enough skill in factorization. | Solutions: The editor and translator use the second method and basically try lots of squares, but we can just use the first method (trial division) for |
sahasraṃ rūpa-saṃyuktaṃ = 1000 + 1 = 1001. |
![]() |
The factors of 4620 — name them quickly, if you have complete proficiency in mathematics. | Solutions: |
vyoma-locana-rasa-abdhayaḥ = 0-2-6-4 = 4620 |
![]() |
Factor 3927. | Solutions: |
śaila-akṣi-nanda-rāma = 7-2-9-3 = 3927. |
![]() ![]() |
|
[A third method starts here.] That is, if In modern terms, the idea is this: write |
That weird glyph that looks like tdṛ = त्दृ is actually is hṛ = हृ :-) |
![]() |
Factors of 120. Factors of 231. | Solution (if we use this third method):
|
kha-netra-indu = 0-2-1 = 120. śaśi-pāvaka-netra = 1-3-2 = 231 |
![]() ![]() |
By a chosen number, take remainders of both numbers being multiplied. Multiply them, and take remainder modulo the chosen number. If this remainder is the same (as that of the product), then the multiplication is [apparently] correct. | This is a “sanity check” for verifying a calculation: it’s saying that if you have a calculation |
hati = multiplication |
![]() |
The editor does the checking mod 3, 5, and 8: mod 3, we can see |
||
![]() |
Here ends vyavahāra (chapter) 11, named bhāgādāna (factorization), in the gaṇita text named Kaumudī, by Nārāyaṇa, who is “sakala-kalā-nidhi narasiṃha-nandana” (son of Narasiṃha), and “gaṇita-vidyā-caturānana”. |
So to summarize, the mathematical content of the chapter is:
- Three methods for factoring numbers:
- Trial division by primes: Divide out factors of
and , then try dividing by , , , etc. - If
, then to until you get a square try adding , etc. When we get a square , then will also be a square, and and are factors of . - If
, find some such that divides . Then is a factor of .
- Trial division by primes: Divide out factors of
- An algorithm for enumerating all divisors of
, from a collected list of prime divisors. - A verification / sanity check procedure for multiplication that generalizes casting out nines, namely to do it modulo some number.
and exercises for each of them. The second method of course is what we call Fermat’s method, named after Pierre de Fermat who independently rediscovered and first described it in a letter in 1643 CE (he must have discovered it some time after 1638):
An odd number not a square can be expressed as the difference of two squares in as many ways as it is the product of two factors, and if the squares are relatively prime the factors are. But if the squares have a common divisor
, the given number is divisible by . Given a number , for examples , to find if it be prime or composite and the factors in the latter case. Extract the square root of . I get , with the remainder . Subtracting the latter from , I get , which is not a square in view of the ending . Hence I add to it. Since the sum is not a square, as seen by the final digits, I again add to it the same number increased by , i.e., , and I continue until the sum becomes a square. This does not happen until we reach , the square of . For by an inspection of the sums mentioned it is easy to see that the final one is the only square (by their endings except for ). To find the factors of , I subtract the first number added, , from the last, . To half the difference add . There results . The sum of and the root is . Adding and subtracting the root of the final sum , we get and , which are the two numbers nearest to whose product is . They are the only factors since they are primes. Instead of additions, the ordinary method of factoring would require the division by all the numbers from to .
A few remarks and links:
-
This Gaṇita-kaumudī was written in 1356 CE (as mentioned above), about 300 years before Fermat came up with the method, and a further 300 years earlier, the author Śrīpati in his Siddhāntaśekhara (1039 CE) gives a limited form of the method: if
and it so happens that is a square say , then giving the two factors and . So Nārāyaṇa can be seen as turning this idea into an actual method for factoring. (Reference: K. S. Shukla, Hindu methods for fnding factors or divisors of a number, Gaṇita, Vol. 17, No. 2 (1966), pp. 109–117, reprinted in “Studies in Indian Mathematics and Astronomy: Selected Articles of Kripa Shankar Shukla” edited by Aditya Kolachana, K. Mahesh, K. Ramasubramanian, Hindustan Book Agency / Springer (2019): https://doi.org/10.1007/978-981-13-7326-8_13) -
To me this example shows that even in the less-notable parts of works, there can be interesting stuff. (The Gaṇita-kaumudī is much more notable for its combinatorics and study of magic squares, and even in the arithmetic parts there are more interesting things like generalizations of Pell’s equation etc.)
-
Fermat was working with 10-digit numbers for which trial division is clearly a lot more work, but it is not clear to me whether Nārāyaṇa had any need for his method (whether he was factoring such large numbers), or it just came about as the result of generally thinking about numbers and factoring. (That is, it is not clear whether the small examples he gives were small just for the sake of exposition.)
-
The third method of Nārāyaṇa is not an explicit algorithm, given the “use your ingenuity” part, and it seems much less useful as a factoring method for large numbers, but I think the underlying idea still has some value / relation to some of the ideas in modern factoring algorithms like the quadratic sieve. I’ll think more about this while writing a future planned post on Cole/Seelhoff etc.
-
Given the huge importance of this work of Nārāyaṇa, you might imagine that there are several manuscripts of this work carefully preserved in several libraries, etc. Alas, the truth is that, as far as I can tell (NCC), all that remain today is two manuscripts of Chapters 13–14, and the text printed in two volumes in 1936 and 1942. The story of the printed edition is this, quoting Padmakara Dvivedi Jyautiśācārya who edited and printed it:
After the death of my revered father [Mahāmahopādhyāya Pandit] Sudhakara Dvivedi, I discovered a complete manuscript of this work in his collection. I immediately set to work upon it…
and (from the first volume)
श्रीपूज्यपादपितृसंगृहीतपुस्तकेषु तेषां निधनात्पश्चान्मया श्रीनृसिंहनन्दननारायणपण्डितरचिताया गणितकौमुद्या एकाशुद्धा हस्तलिखतप्रतिः प्राप्ता। लुप्तप्रायस्य सुतरां कुत्राप्यमुद्रितपूर्वस्य गणितकौमुदीग्रन्थस्यायं प्रथमप्रकाशनावसरः।
(śrī-pūjyapāda-pitṛ-saṃgṛhīta-pustakeṣu teṣāṃ nidhanāt paścān mayā śrī-nṛsiṃha-nandana-nārāyaṇa-paṇḍita-racitāyā gaṇita-kaumudyā ekāśuddhā hasta-likhata-pratiḥ prāptā. lupta-prāyasya sutarāṃ kutrāpy amudrita-pūrvasya gaṇita-kaumudī-granthasyāyaṃ prathama-prakāśanāvasaraḥ.)
So consider all the things that had to go right, for this work not to be lost:
-
Pandit Sudhakara Dvivedi had a copy of the manuscript in his collection,
-
His son Padmakara Dvivedi (Jyautiśācārya) was also a scholar in the same tradition,
-
He happened to recognize the manuscript and its importance, and had the energy and ability to edit it and print it (with his commentary/notes),
-
There was a publisher at the time (1936 and 1942) that considered the work worth printing.
-
Enough libraries carried copies of this printed book that scans of it are available to us today.
And this is a pretty much a “best case” scenario. How many other manuscripts were lost, as traditional learning declined, or children were not up to publishing works from their parents' collections, or could not find a publisher? As it is, the printed version is based on a single complete manuscript (though the preface to the first volume mentions another manuscript from Nepal, found after printing), which for all I can tell may not even be available any more. This is the fate of one of the major works of Indian mathematics. See also the article The Untapped Wealth Of Manuscripts On Indian Astronomy And Mathematics by M. D. Srinivas (here / here). (Maybe there’s hope: there’s a listing in the National Mission for Manuscript’s database, so maybe a manuscript still exists after all?)
-
-
As mentioned above, the work has been printed, and there are some scans of this work online:
-
An English translation (based on the printed book) was published by Paramanand Singh across several issues of the Gaṇita-bhāratī journal (Bull. Ind. Soc. Hist. Math.) during 1998–2002:
- 20 (1998): 25–82 (Chaps I–III);
- 21 (1999): 10–73 (Chap. IV);
- 22 (2000): 19–85 (Chaps V–XII);
- 23 (2001): 18–82 (Chap XIII); and
- 24 (2002): 35–98 (Chap. XIV).
The journal is hard to find but someone has kindly compiled the translation of Chapters 1 to 13 and uploaded the translation here (translation of Chap 14 is mising).
-
There are three lectures on the Gaṇita-kaumudī in the NPTEL course on “Mathematics in India - From Vedic Period to Modern Times” (YouTube playlist):
- Lecture 25, by Prof. M. S. Sriram
- Lecture 26, by Prof. M. S. Sriram
- Lecture 27, by Prof. M. D. Srinivas — this one contains the factoring parts.
-
Many sources mention that the Gaṇita-kaumudī comes with both a mūla (source) text and a vāsanā (commentary) written by Nārāyaṇa himself; the latter would be very valuable and it’s not clear to me whether the latter has been published or is available anywhere.
Edit: Shortly after I wrote this post, I noticed that quite by coincidence, the January 2023 issue of the excellent mathematics journal Bhāvanā has a long article on the Gaṇitakaumudī of Nārāyaṇa Paṇḍita and its highlights. It doesn’t even touch on this factoring chapter, which should give a sense of how much more there is in this work! The article is by Prof. K. Ramasubramanian and a couple of PhD-student co-authors (Prasad Jawalgekar, D.G. Sooryanarayan), and there is great news:
The authors of this article are currently engaged in bringing out a revised edition, with translation, and a detailed mathematical exposition of the contents of Gaṇitakaumudī.