Skip to content

Pi Day – Calculating Pi

So it’s Pi day and the one question you may be wondering is “how is Pi calculated?”

Measuring circles can give a rough estimate, but an infinite series is perhaps a better and more common way to do it. As described on Calculating Pi at, the Nilakantha Series works pretty well. Its formula looks like this:

Nilakantha Series: π 4 / (2×3×4) − 4 / (4×5×6) 4 / (6×7×8) − 4 / (8×9×10) 

You ought to be able to figure out a pattern from that. And it’s a short leap to convert that to a Xojo app. Here’s code that calculates Pi using the Nilakantha Series:

 Dim even As Boolean = True
 Dim calc As Double
 Dim calcStart As Int64
 Dim iterations As Integer
 While iterations < 1000
   iterations = iterations + 1
   calcStart = calcStart + 2
   calc = 4.0 / (calcStart * (calcStart + 1) * (calcStart + 2))
   If even Then
     Pi = Pi + calc
     Pi = Pi - calc
   End If
   even = Not even

Here’s a completed project you can run:

CalcPi project

This correctly calculated the first 9 digits of Pi almost immediately. Unfortunately, calculating more digits of Pi is not really practical using the standard double-precision floating point data type used in most programming languages.