The most obvious expression for computing π in APL is ○1
. But what if you can’t remember how ○
works, or your O key is broken, or you feel like taking the road less travelled? With thanks to Wikipedia’s excellent list of Approximations of π, here are some short sweet APL expressions for three-and-a-bit:
3 ⍝ very short
34 ⍝ not so sweet
4s←*∘0.5 ⍝ let's allow ourselves some square roots
+/s 2 3
3.1462643699419723423291350657155731*÷3
3.141380652391393004493075896462748+/1.8*1 0.5 ⍝ Ramanujan
3.141640786499873817845504201238766s 7+s 6+s 5
3.141632544503617840472137945142766÷/7 4*7 9
3.1415672302246093759⍟995
3.141573605337628094187009177086444355÷113
3.141592920353982300884955752212389s s 2143÷22 ⍝ Ramanujan again
3.141592652582646125206037179644022+∘÷/3 7 15 1 292 ⍝ continued fraction
3.14159265301190260407226149477373÷/63 25×17 7+15×s 5
3.14159265380568820189839000630151(1E100÷11222.11122)*÷193
3.141592653643822210363178893440074(⍟744+640320*3)÷s 163 ⍝ Ramanujan yet again
3.141592653589793238462643383279727
This last one is accurate to more places than I ever learned in my youth!
Technical note: to get plenty of precision, these examples were evaluated with 128-bit decimal floating-point, by setting ⎕FR←1287
and ⎕PP←34
.
For more on continued fractions, see cfract in the dfns workspace.