← All writing Ancient algorithms, 15 min read

The Madhava-Leibniz series for pi, with Madhava's correction terms, in Python

The Madhava-Leibniz series for pi from the Sanskrit verses: a word-by-word translation, Madhava's end corrections, tested Python and a convergence benchmark.

Sum 100 terms of 1 - 1/3 + 1/5 - 1/7 + ..., multiply by 4, and your value of pi is still off by about 0.01. Kerala astronomers had a fix. Sanskrit texts written there in the 16th century credit Madhava (Mādhava) of Sangamagrama, who lived c. 1340 to 1425, with this series, now called the Madhava-Leibniz series, and with 3 end corrections that estimate the part you leave out. With the third correction, the same 100 terms bring the error below 10^-14.

In Europe, James Gregory found the arctangent series in 1671 and Gottfried Leibniz the pi/4 case in 1673. I took the Sanskrit verses that state the series and the corrections, translated them word by word, worked an example, and coded all of it in Python with tests. The code is on GitHub, and this is post 2 of Ancient algorithms, implemented, after the kuttaka.

What the Madhava-Leibniz series computes

Indian astronomers needed the circumference of a circle from its diameter, to turn arcs into sines and back. Aryabhata (499 CE) used 62832/20000 = 3.1416. Bhaskara II's Lilavati (1150) gives 3927/1250, the same value. Kerala astronomers wanted more digits:

  • Verses quoted in the Yuktidipika (chapter 2, verse 298) give 104348/33215, which is pi to 9 decimal places. Whish (1834) printed them as part of the Tantrasangraha.
  • The Sadratnamala of Sankaravarman (1819) gives pi rounded to 17 decimal places, written in the katapayadi letter code.

An infinite series can give as many digits as you are willing to compute. The series here is

C4d=1−13+15−17+⋯\frac{C}{4d} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots

where C is the circumference and d the diameter. The texts state it for the circumference, so every term carries a factor 4d.

The series converges slowly. The error after n terms is close to 1/n, so each new correct digit costs 10 times as many terms. 10,000 terms bring the error below 10^-4, and a million terms below 10^-6. Madhava's end corrections stop early and estimate everything skipped with 1 fraction.

Who was Madhava of Sangamagrama, and who wrote the series down?

No surviving work by Madhava contains the series or the corrections. The works that survive under his name, such as the Venvaroha (with an epoch in 1400 CE) and the Sphutacandrapti, do not state them. The series reaches us through later Kerala authors:

  • Nilakantha Somayaji (1444 to 1545), author of the Tantrasangraha, completed in 1500. K. V. Sarma, who edited it in 1977, dates it from chronograms in the text.
  • Shankara Variyar (c. 1500 to 1560), who wrote the Yuktidipika, a commentary in Sanskrit verse on the Tantrasangraha, and the Kriyakramakari, a prose commentary on Bhaskara II's Lilavati. Sarma dates his works to between 1529 and 1556, from dates written in them.
  • Jyeshthadeva (c. 1500 to 1610 in Sarma's estimate), who wrote the Yuktibhasa in Malayalam prose. It derives the series and argues for the first 2 corrections.

Here is what each text says about authorship, as far as I could check:

  • The Yuktidipika (chapter 2, verses 271 to 274 and 295 to 296) quotes the series and corrections in quotation marks, followed by iti, the Sanskrit marker for "end of quotation". In Sarma's edition no name stands next to these verses.
  • The Kriyakramakari introduces the same rule as "an easier way to get the circumference", mentioned by Madhava. That is Kim Plofker's translation (2009), as quoted on Wikipedia; I have not seen the book or the Sanskrit.1
  • Modern accounts. Krishnachandran (2024) states that all 3 corrections are "attributed to Mādhava" in Kerala texts.

Madhava's dates, c. 1340 to 1425, are an inference. They rest on his pupil Parameshvara (c. 1360 to 1455), on the 1400 CE epoch of the Venvaroha, and on Sarma's study of the Kerala astronomers. Charles Whish, an East India Company official in Madras, first described these series to European readers in a paper read in December 1832 and published in 1834.

The verses

The verses below are from the Yuktidipika, chapter 2, verses 271 to 274, in K. V. Sarma's 1977 edition, page 101. I checked the text against the printed page. Whish (1834) identified the meter as giti.

व्यासे वारिधिनिहते रूपहृते व्याससागराभिहते ।
त्रिशरादिविषमसंख्याभक्तमृणं स्वं पृथक् क्रमात् कुर्यात् ॥ २७१ ॥

यत्संख्ययात्र हरणे कृते निवृत्ता हृतिस्तु जामितया ।
तस्या ऊर्ध्वगता या समसंख्या तद्दलं गुणोऽन्ते स्यात् ॥ २७२ ॥

तद्वर्गो रूपयुतो हारो व्यासाब्धिघाततः प्राग्वत् ।
ताभ्यामाप्तं स्वमृणे कृते धने क्षेप एव करणीयः ॥ २७३ ॥

लब्धः परिधिः सूक्ष्मो बहुकृत्वो हरणतोऽतिसूक्ष्मः स्यात् ॥ २७४ ॥

vyāse vāridhinihate rūpahṛte vyāsasāgarābhihate |
triśarādiviṣamasaṃkhyābhaktam ṛṇaṃ svaṃ pṛthak kramāt kuryāt || 271 ||

yatsaṃkhyayātra haraṇe kṛte nivṛttā hṛtis tu jāmitayā |
tasyā ūrdhvagatā yā samasaṃkhyā taddalaṃ guṇo 'nte syāt || 272 ||

tadvargo rūpayuto hāro vyāsābdhighātataḥ prāgvat |
tābhyām āptaṃ svam ṛṇe kṛte dhane kṣepa eva karaṇīyaḥ || 273 ||

labdhaḥ paridhiḥ sūkṣmo bahukṛtvo haraṇato 'tisūkṣmaḥ syāt || 274 ||

Word by word

The numbers are written as words, a system called bhutasankhya. Varidhi, sagara and abdhi all mean "ocean" and stand for 4, the traditional count of oceans. Rupa, "form", stands for 1. Tri is 3, and shara, "arrow", stands for 5.

SanskritPartsMeaning
vyāse vāridhinihatevyāsa + vāridhi + nihatathe diameter multiplied by 4 (oceans)
rūpahṛterūpa + hṛtadivided by 1
vyāsasāgarābhihatevyāsa + sāgara + abhihatathe diameter multiplied by 4 (oceans)
triśarādiviṣamasaṃkhyābhaktamtri + śara + ādi + viṣama + saṃkhyā + bhaktadivided by the odd numbers 3, 5 and so on
ṛṇaṃ svaṃṛṇa, svanegative, positive
pṛthak kramāt kuryātone should apply separately, in order
yatsaṃkhyayā atra haraṇe kṛteyad + saṃkhyā, haraṇa, kṛtawhen division has been done by whichever number
nivṛttā hṛtiḥ tu jāmitayānivṛtta, hṛti, jāmitāthe dividing stops, from weariness
tasyā ūrdhvagatā yā samasaṃkhyāūrdhva + gata, sama + saṃkhyāthe even number that comes after it
taddalaṃ guṇo 'nte syāttad + dala, guṇa, antaits half is the multiplier at the end
tadvargo rūpayuto hāraḥtad + varga, rūpa + yuta, hāraits square plus 1 is the divisor
vyāsābdhighātataḥ prāgvatvyāsa + abdhi + ghātaof the diameter times 4 (ocean), as before
tābhyām āptamwhat is obtained from those 2
svam ṛṇe kṛtesva, ṛṇa, kṛtais positive when (the last term) was made negative
dhane kṣepa eva karaṇīyaḥdhana, kṣepa, karaṇīyawhen positive, ... is to be made (see below)
labdhaḥ paridhiḥ sūkṣmaḥlabdha, paridhi, sūkṣmathe circumference obtained is accurate
bahukṛtvo haraṇataḥ atisūkṣmaḥ syātbahu + kṛtvas, haraṇa, ati + sūkṣmafrom dividing many times, it becomes very accurate

My translation

271. Multiply the diameter by 4 and divide by 1. Multiply the diameter by 4, divide separately by the odd numbers 3, 5 and so on, and apply the results in turn as negative and positive.

272. When you stop dividing, out of weariness, at some number, take the even number that comes after it. Half of that is the multiplier for the end.

273. The square of that even number, plus 1, is the divisor of the diameter times 4, as before. What these 2 give is added when the last term was subtracted, and subtracted when it was added.

274. The circumference obtained is accurate. From dividing many times, it becomes very accurate.

The last half of verse 273 reads dhane kṣepa eva karaṇīyaḥ in Sarma's edition, and kṣepa normally means "addition". The sense, and every translation I have seen, needs a subtraction there. Whish printed this half-line in 1834 as śodhanañ ca karaṇīyam, "and a subtraction is to be made", which fits. My translation follows the sense.

Whish's 1834 translation, which is in the public domain, reads:

Multiply the diameter by 4, and from it subtract and add alternately the quotients obtained by dividing four times the diameter by the odd numbers 3, 5, 7, 9, 11, &c, do thus to the extent required; and having fixed a limit, take half the even number next less than the last odd divisor for a multiplier, and its square plus one for a divisor. Multiply four times the diameter by the multiplier, and divide the product by the divisor, and add it or subtract it, according to the sign of the last quote in the series, from the sum of the series thus the circumference of the given diameter will be obtained very correctly.

C. M. Whish, Transactions of the Royal Asiatic Society 3 (1834), p. 512

Whish's "next less than" does not match the verse, which says the even number "that has gone above", the next one up. With the even number below, the example in the next section comes out at 3.094 instead of 3.14166. In his translation of the third correction, Whish uses the even number above.

The third correction is in verses 295 and 296, on page 103 of Sarma's edition:

एभ्यः सूक्ष्मतरोऽन्यो विलिख्यते कश्चनापि संस्कारः ।
अन्ते समसंख्यादलवर्गः सैको गुणः स एव पुनः ॥ २९५ ॥
युगगुणितो रूपयुतः समसंख्यादलहतो भवेद् हारः ।
त्रिशरादिविषमसंख्याहरणात् परमेतदेव वा कार्यम् ॥ २९६ ॥

ebhyaḥ sūkṣmataro 'nyo vilikhyate kaścanāpi saṃskāraḥ |
ante samasaṃkhyādalavargaḥ saiko guṇaḥ sa eva punaḥ || 295 ||
yugaguṇito rūpayutaḥ samasaṃkhyādalahato bhaved dhāraḥ |
triśarādiviṣamasaṃkhyāharaṇāt param etad eva vā kāryam || 296 ||

295 to 296. Another correction, finer than these, is now written. At the end, the square of half the even number, plus 1, is the multiplier. That same number times 4 (yuga, the 4 ages), plus 1, times half the even number, is the divisor. After dividing by the odd numbers 3, 5 and so on, apply this instead.

What the verses leave open

  1. Who wrote them. The Yuktidipika quotes both sets of verses without naming an author. The credit to Madhava comes from the Kriyakramakari and from the later tradition.
  2. The first correction. No verse states 1/(4n) by itself. The Yuktibhasa reaches it as a first guess on the way to the second, which is why it counts as a correction at all.
  3. Where the corrections come from. The Yuktibhasa argues for the first 2. No surviving text explains the third. Historians have proposed reconstructions, which I list under "What is debated".
  4. The sign. Verse 273 gives the rule for adding or subtracting, with the textual problem above. The code follows the sense: the correction always has the sign opposite to the last term used.

Working an example

Take a diameter of 1 and stop after 5 terms, so the last divisor is 9.

Step 1. The series. 4 - 4/3 + 4/5 - 4/7 + 4/9 = 1052/315, which is 3.33968, off by 0.198.

Step 2. The multiplier and divisor. The even number after 9 is 10. Half of it, 5, is the multiplier. The even number's square plus 1, 10^2 + 1 = 101, is the divisor.

Step 3. The correction. 4 × 5 / 101 = 20/101, which is 0.19802.

Step 4. The sign. The last term, 4/9, was added, so subtract: 1052/315 - 20/101 = 99952/31815, which is 3.14166.

Pi is 3.14159, so 5 terms and 1 fraction bring the error down to 0.00007, below 10^-4. The bare series needs about 10,000 terms for that.

In terms of n, the number of terms, the multiplier is n and the divisor is 4n^2 + 1. Divide by 4d to get the 3 corrections for pi/4:

CorrectionAfter n termsSource
11/(4n)Yuktibhasa, as a first step
2n/(4n^2 + 1)Yuktidipika 2.271 to 274
3(n^2 + 1)/(4n^3 + 5n)Yuktidipika 2.295 to 296

Why the corrections work

Call f(p) the exact correction you would add after stopping at the odd divisor p. Stop at p - 2 instead and the correction must make up for the term 1/p as well. So the true correction satisfies

f(p−2)+f(p)=1pf(p-2) + f(p) = \frac{1}{p}

The Yuktibhasa argues from this condition. If f were the same at p - 2 and p, each would be 1/(2p). But a single rule has to apply at every stopping point, so try the value at the midpoint, the even number p + 1:

f1(p)=12(p+1)=14nf_1(p) = \frac{1}{2(p+1)} = \frac{1}{4n}

Put f_1 into the condition and the 2 sides miss by 1/(p^3 - p), which the texts call the sthaulya, the "grossness". Choosing the denominator to make that miss smaller gives the second correction, whose sthaulya is 4/(p^5 + 4p). Ramasubramanian writes the 3 denominators as

2(p+1),2(p+1)+42(p+1),2(p+1)+42(p+1)+162(p+1)2(p+1), \qquad 2(p+1) + \frac{4}{2(p+1)}, \qquad 2(p+1) + \cfrac{4}{2(p+1) + \cfrac{16}{2(p+1)}}

Each is the next step of a continued fraction, and with p = 2n - 1 they turn into the 3 formulas in the table above. Each step makes the error fall faster as n grows. The tests check that doubling n divides the error by about 8, 32 and 128 for corrections 1, 2 and 3, so the errors fall as n^-3, n^-5 and n^-7.

The Python

This is the rule from the verses, with exact fractions:

from fractions import Fraction


def madhava_circumference(diameter: int, terms: int, correction: int = 2) -> Fraction:
    """4d(1 - 1/3 + 1/5 - ...) to `terms` terms, plus one of Madhava's end corrections."""
    total = Fraction(0)
    for k in range(terms):
        term = Fraction(4 * diameter, 2 * k + 1)
        total += term if k % 2 == 0 else -term
    n = terms
    tail = {
        0: Fraction(0),
        1: Fraction(1, 4 * n),
        2: Fraction(n, 4 * n * n + 1),
        3: Fraction(n * n + 1, 4 * n**3 + 5 * n),
    }[correction]
    sign = 1 if n % 2 == 0 else -1  # subtract after an added last term
    return total + sign * 4 * diameter * tail


print(float(madhava_circumference(1, 5, 0)))  # 3.33968253968254
print(float(madhava_circumference(1, 5)))  # 3.1416627377023416
print(float(madhava_circumference(1, 20, 3)))  # 3.1415926540198638

The repo version works in decimal.Decimal at a precision you choose, so it can measure the error against a 100-place reference value. It also has the faster series pi = sqrt(12) × (1 - 1/(3 × 3) + 1/(5 × 3^2) - ...), which the Yuktidipika also states (chapter 2, verse 212), and John Machin's 1706 formula for comparison. The tests check:

  • each correction beats the one before it at 10, 20, 40 and 80 terms;
  • doubling n divides the error by 2^3, 2^5 and 2^7, within half a power of 2;
  • 50 terms with the third correction give an error below 10^-12;
  • Machin's formula with 41 terms reproduces pi to the 60 significant digits the module works at.

Against the modern methods

Here is the benchmark from the repo, benchmarks/madhava_convergence.py, computed at 120 significant digits. Each cell is the largest k with the error below 10-k. That can be 1 more than the number of decimals that agree: 100 terms of the bare series give 3.1316, an error of 0.0099998, so the cell says 2 while only 1 decimal agrees.

Terms125102050100
Bare series0001112
+ correction 10123456
+ correction 212457910
+ correction 3125791214
sqrt(12) series0125102549
Machin (1706)137142971100

The 100 in the Machin column is the precision of the reference value, so it is a floor.

Step chart of the largest k with the error in pi below 10^-k, after 1 to 100 terms of the series. At 100 terms the bare series reaches 2, correction 1 reaches 6, correction 2 reaches 10 and correction 3 reaches 14. Step chart of the largest k with the error in pi below 10^-k, after 1 to 100 terms of the series. At 100 terms the bare series reaches 2, correction 1 reaches 6, correction 2 reaches 10 and correction 3 reaches 14.

Machin's formula, pi/4 = 4 arctan(1/5) - arctan(1/239), uses the same arctangent series at small arguments, so each term shrinks by a factor of 25 or more.

The Kerala texts also contain faster series. Whish printed one in 1834, C = 16d (1/(1^5 + 4) - 1/(3^5 + 12) + 1/(5^5 + 20) - ...), which is in the Yuktidipika at chapter 2, verses 287 to 288. Krishnachandran (2024) shows how series of this kind follow from the correction terms.

Parallels and lineage

The connections I can support from sources:

  • James Gregory, 1671. Gregory found the arctangent series in 1671, by what we now call Taylor series (Roy 1990).
  • Gottfried Wilhelm Leibniz, 1673 to 1676. Roy (1990) dates Leibniz's discovery of the pi/4 series to 1673, by a transformation of areas. Leibniz sent it to Christiaan Huygens, and Isaac Newton praised it in a letter to Henry Oldenburg of 24 October 1676.
  • Kerala, 14th to 16th century. The same series, with the corrections, in the texts above. The same texts give series for sine and cosine, which Ramasubramanian (2022) lists among the Kerala results.

What is debated

  • Transmission to Europe. Unproven. I found no published document that shows the series passing from Kerala to Gregory or Leibniz, and Roy (1990) treats the 3 discoveries as independent. C. K. Raju (Cultural Foundations of Mathematics, 2007) argues that the calculus passed from India to Europe in the 16th century. I have not read the book, so I note the claim without assessing it.
  • Who found the series. Roy (1990) wrote that the Indian author was "usually ascribed" to be Nilakantha, and that his identity was "not definitely known". Later work, using the Kriyakramakari, settles on Madhava. No text in Madhava's own hand survives to confirm it.
  • How the third correction was found. The texts give no rationale for it. Hayashi, Kusuba and Yano (1990) proposed that all 3 corrections came from a known value of pi, such as 355/113, worked through a Euclidean-style division. Krishnachandran (2024) summarizes this and shows the same derivation works from Aryabhata's 62832/20000.
  • Madhava's home. Sarma and others place Sangamagrama at Irinjalakuda. Krishnachandran (2024) argues for Kudallur, on the Nila river, whose Malayalam name means "confluence village".
  • The reading of verse 273. The kṣepa problem above.

Key takeaways

  • pi/4 = 1 - 1/3 + 1/5 - ... is stated in Sanskrit verse in Shankara Variyar's Yuktidipika (16th century), which quotes it without a name. The Kriyakramakari credits it to Madhava (c. 1340 to 1425). None of Madhava's surviving works contains it.
  • The verses add an end correction: after stopping, add or subtract 4d × n/(4n^2 + 1). A second verse gives a finer one, 4d × (n^2 + 1)/(4n^3 + 5n).
  • With 20 terms, the bare series is off by 0.05 and the third correction by less than 10^-9.
  • Gregory (1671) and Leibniz (1673) found the series in Europe. No documented route links them to Kerala.
  • The code, tests and benchmark are in ancient-algorithms. Next in the series: Pingala's enumeration of meters.

Sources

  • Nilakantha, Tantrasangraha, with the Yuktidipika and Laghuvivrti of Shankara, edited by K. V. Sarma. Vishveshvaranand Vedic Research Institute, Hoshiarpur, 1977. Chapter 2, verses 271 to 274 (p. 101) and 295 to 296 (p. 103). Scan on archive.org.
  • C. M. Whish, "On the Hindú quadrature of the circle". Transactions of the Royal Asiatic Society of Great Britain and Ireland 3 (1834), pp. 509 to 523.
  • K. Plofker, Mathematics in India. Princeton University Press, 2009. Not read; quoted through Wikipedia's "Kriyakramakari" article.
  • R. Roy, "The discovery of the series formula for pi by Leibniz, Gregory and Nilakantha". Mathematics Magazine 63(5), 1990, pp. 291 to 306.
  • V. N. Krishnachandran, "On Mādhava and his correction terms for the Mādhava-Leibniz series for π", arXiv 2405.11134, 2024.
  • K. Ramasubramanian, "The history and historiography of the discovery of calculus in India", ICM 2022 lecture slides.
  • T. Hayashi, T. Kusuba and M. Yano, "The correction of the Mādhava series for the circumference of a circle". Centaurus 33, 1990, pp. 149 to 174. Not read; summarized from Krishnachandran (2024).

Footnotes

  1. I have this attribution only through Plofker's translation as quoted on Wikipedia. I have not seen Plofker's book or Sarma's 1975 edition of the Kriyakramakari. ↩

Common questions

What is the Madhava-Leibniz series?↓

It is the infinite series pi/4 = 1 - 1/3 + 1/5 - 1/7 + ..., the arctangent series at x = 1. Sanskrit texts from 16th-century Kerala state it and credit it to Madhava of Sangamagrama (c. 1340 to 1425). In Europe, James Gregory found the arctangent series in 1671 and Gottfried Leibniz found the pi/4 series in 1673.

What are Madhava's correction terms?↓

They estimate the tail of the series after you stop. If the last term used has denominator 2n - 1, the 3 corrections for pi/4 are 1/(4n), n/(4n^2 + 1) and (n^2 + 1)/(4n^3 + 5n). The Yuktidipika states the second and third in verse. With 20 terms they bring the error below 10^-4, 10^-7 and 10^-9, against 10^-1 for the bare series.

Who is the father of mathematics in Kerala?↓

Madhava of Sangamagrama. Modern writers call him the founder of the Kerala school of astronomy and mathematics, for example V. N. Krishnachandran (2024), and K. Ramasubramanian's ICM 2022 lecture calls him its pioneer. Later Kerala authors refer back to him: K. V. Sarma (1977) notes that Nilakantha (born 1444) quotes Madhava often, and Shankara Variyar's Kriyakramakari credits the pi series to him.

Did Leibniz copy the series from India?↓

No published document I found shows the series traveling from Kerala to Europe. Transmission is unproven, and historians still debate it.

Share this
← More writing Get in touch