Methods for factoring numbers, in the Gaṇitakaumudī of Nārāyaṇa Paṇḍita
The Gaṇitakaumudī (गणितकौमुदी) is a work on mathematics written by Nārāyaṇa Paṇḍita in 1356 CE. Also known as the Gaṇitapāṭī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 $N$ is not even nor has $5$ in the units place, try the prime numbers—3, 7, 11, etc—as divisors. 


While you find divisors, keep finding divisors.  hara = divisor 

If $N$ is a square, then its root is a factor, twice.  [An alternative method starts here…] 


When $N$ is not a square, when added to some square it becomes a square. Then, the sum and difference of the two squareroots are factors.  That is, there exists some square $s^2$ such that $N + s^2$ is a square, say $N + s^2 = b^2$. Then $b  s$ and $b+ s$ are factors of $N$. 


Of these numbers, as before, find factors.  That is, factorize the $bs$ and $b+s$ further. 
rāśi = quantity, number — a number that has been written down, e.g. an output of a previous step 

Of a nonsquare number, find the nearest (integer) square root, double it and add 1…  
…and subtract from it the squareremainder. If the result is a square, then this is the number to be added.  That is, let $N = m^2 + r$. Then if $2m + 1  r$ is a square say $s^2$, then $N + s^2$ is $(m^2 + r) + (2m + 1  r)$ which is $(m + 1)^2$, so we are done, as we have found a square $s^2$ such that $N + s^2$ is a square. 

If it is not a square, then further add $2m + 3$…  The idea is that we’re trying successive values for the smaller square: if $(m + 1)^2  N$ isn’t a square, then the next option would be to replace $(m + 1)^2$ with $(m + 2)^2$, which is $(m + 1)^2 + (2m + 3)$. 

…and so on in arithmetic progression, until you get a square.  So to $2m + 1  r$, we’re adding $2m+3$, then $2m+5$ and $2m+7$ and so on. This way, we’re trying to write $N = a^2  s^2$ by trying, for $a$, successive values $m+1$ then $m+2$ and so on until we find a square $b^2$, but without having to square each time. (We do need to test squareness each time, though.) 
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 $7520 = 2^5 \times 5 \times 47$, and $10201 = 101^2$. 


List the divisors of 1161, if you’re a mathematician who knows how to factor.  Solutions: It turns out that $1161 = 3^3 \times 43$, which we could also get alternatively with $1161 = 34^2 + 5$ and $2\times34 + 1  5 = 64$ which is a square, so $1161 = 35^2  8^2 = 27 \times 43$. 
candraaṅgabhūbhuvaḥ = 1611 = 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 $1001 = 7 \times 11 \times 13$, no? 
sahasraṃ rūpasaṃyuktaṃ = 1000 + 1 = 1001. 

The factors of 4620 — name them quickly, if you have complete proficiency in mathematics.  Solutions: $4620 = 2^2 \times 3 \times 5 \times 7 \times 11$. So there are 48 factors (or 47 without the 1). Again the editor factorizes 4620 by first removing the factors of 2 and 5 and then factorizing $3 \times 7 \times 11 = 231$ using the differenceofsquares method… but why?! 
vyomalocanarasaabdhayaḥ = 0264 = 4620 

Factor 3927.  Solutions: $3927 = 3 \times 7 \times 11 \times 17$. 
śailaakṣinandarāma = 7293 = 3927. 


[A third method starts here.] That is, if $N = m^2 + r$, then for some unknown $x$, it is the case that $m  x$ divides $N$. (We want to find $x$.) If we find an $x$ such that $x^2 + r$ is divisible by $m  x$, then we have found $m  x$ as a factor of $N$. (This is actually nontrivial!) In modern terms, the idea is this: write $N = m^2 + r$, and we’re trying to find some $p = mx$ that divides $N$. Then, $m = p + x \equiv x \pmod p$, and so $N = m^2 + r \equiv x^2 + r \pmod p$. So we want $p = m  x$ to divide $x^2 + r$. That’s the rule. 
That weird glyph that looks like tdṛ = त्दृ is actually is hṛ = हृ :) 

Factors of 120. Factors of 231.  Solution (if we use this third method):

khanetraindu = 021 = 120. śaśipāvakanetra = 132 = 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 $a \times b = c$ and you check that $(a \bmod m) \times (b \bmod m) \equiv (c \bmod m)$, then likely the calculation is correct. 
hati = multiplication 

$29 \times 17 = 493$ — check this.  The editor does the checking mod 3, 5, and 8: mod 3, we can see $2 \times 2 \equiv 4$. Mod 5, we can see $4 \times 2 \equiv 3$. Mod 8, we can see $5 \times 1 \equiv 5$. The calcuation is likely correct. (In modern terms, we could conclude that the multiplication is correct mod $120$, by the Chinese Remainder Theorem.) 

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 “sakalakalānidhi narasiṃhanandana” (son of Narasiṃha), and “gaṇitavidyā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 $2$ and $5$, then try dividing $N$ by $3$, $7$, $11$, $13$ etc.
 If $N = m^2 + r$, then to $2m  1 + r$ until you get a square try adding $2m + 3$, $2m + 5$ etc. When we get a square $s^2$, then $N + s^2 = b^2$ will also be a square, and $b  s$ and $b + s$ are factors of $N$.
 If $N = m^2 + r$, find some $x$ such that $m  x$ divides $x^2 + r$. Then $m  x$ is a factor of $N$.
 An algorithm for enumerating all divisors of $N$, 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 $d$, the given number is divisible by $\sqrt{d}$. Given a number $n$, for examples $2027651281$, to find if it be prime or composite and the factors in the latter case. Extract the square root of $n$. I get $r = 45029$, with the remainder $40440$. Subtracting the latter from $2r + 1$, I get $49619$, which is not a square in view of the ending $19$. Hence I add $90061 = 2+ 2r+ 1$ to it. Since the sum $139680$ is not a square, as seen by the final digits, I again add to it the same number increased by $2$, i.e., $90063$, and I continue until the sum becomes a square. This does not happen until we reach $1040400$, the square of $1020$. 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 $499944$). To find the factors of $n$, I subtract the first number added, $90061$, from the last, $90081$. To half the difference add $2$. There results $12$. The sum of $12$ and the root $r$ is $45041$. Adding and subtracting the root $1020$ of the final sum $1040400$, we get $46061$ and $44021$, which are the two numbers nearest to $r$ whose product is $n$. They are the only factors since they are primes. Instead of $11$ additions, the ordinary method of factoring would require the division by all the numbers from $7$ to $44021$.
A few remarks and links:

This Gaṇitakaumudī 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 $N = m^2 + r$ and it so happens that $2m + 1  r$ is a square say $b^2$, then $N = (a + 1)^2  b^2$ giving the two factors $a + 1  b$ and $a + 1 + b$. 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/9789811373268_13)

To me this example shows that even in the lessnotable parts of works, there can be interesting stuff. (The Gaṇitakaumudī 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 10digit 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ādapitṛsaṃgṛhītapustakeṣu teṣāṃ nidhanāt paścān mayā śrīnṛsiṃhanandananārāyaṇapaṇḍitaracitāyā gaṇitakaumudyā ekāśuddhā hastalikhatapratiḥ prāptā. luptaprāyasya sutarāṃ kutrāpy amudritapūrvasya gaṇitakaumudīgranthasyāyaṃ prathamaprakāś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ṇitabhā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ṇitakaumudī 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ṇitakaumudī 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 PhDstudent coauthors (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ī.