Three-and-a-bit

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
3
      4                                 ⍝ not so sweet
4
      s←*∘0.5                           ⍝ let's allow ourselves some square roots
      +/s 2 3
3.14626436994197234232913506571557
      31*÷3
3.141380652391393004493075896462748
      +/1.8*1 0.5                       ⍝ Ramanujan
3.141640786499873817845504201238766
      s 7+s 6+s 5
3.141632544503617840472137945142766
      ÷/7 4*7 9
3.141567230224609375
      9⍟995
3.141573605337628094187009177086444
      355÷113
3.141592920353982300884955752212389
      s 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.

5 thoughts on “Three-and-a-bit

  1. I seem to recall that I was taught to use
    22÷7
    3.1428571428571428571429

    as a good approximation .. that was in slide rule days ..

  2. At 30-decimal places, you can specify a point on the rim of our galaxy, the Milky Way, to pretty much the nearest hydrogen atom!

    • Depending on what you mean by “pretty much the nearest hydrogen atom”, 30 decimal place may not suffice.

      The diameter of Milky Way galaxy is 1e5 to 1.2e5 light years. Therefore, its circumference is π × 1e5 light-years × 1e16 m/light-year × 1e12 picometers/m = 3.14e33 pm. The atomic radius of hydrogen is 53 pm.

  3. 0j¯1 × ⍟¯1
    3.141592653589793

    Since I want to mention Euler’s identity at every opportunity ☺

    (* 0j1 × π) = ¯1 Euler’s identity, the most beautiful equation in all of mathematics
    (⍟ * 0j1 × π) = ⍟¯1 take log of both sides
    (0j1 × π) = ⍟¯1 ⍟ and * are an inverse pair
    π = (⍟¯1)÷0j1 divide both sides by 0j1
    π = 0j¯1×⍟¯1 ÷0j1 is 0j¯1

    This expression satisfies the stated requirements:

    • can’t remember how ○ works
    • the O key was broken
    • take road less travelled

  4. (Trying again as the blogging software made a hash of my formatting.)

    0j¯1 × ⍟¯1
    3.141592653589793

    Since I want to mention Euler’s identity at every opportunity ☺

    (* 0j1 × π) = ¯1
    Euler’s identity, the most beautiful equation in all of mathematics

    (⍟ * 0j1 × π) = ⍟¯1  
    take log of both sides

    (0j1 × π) = ⍟¯1
    ⍟ and * are an inverse pair

    π = (⍟¯1)÷0j1
    divide both sides by 0j1

    π = 0j¯1×⍟¯1
    ÷0j1 is 0j¯1

    This expression satisfies the stated requirements:

    • can’t remember how ○ works
    • the O key was broken
    • take road less travelled