Skip to content

Instantly share code, notes, and snippets.

@alexamies
Created February 17, 2020 18:57
Show Gist options
  • Save alexamies/063ba8e62fc008fa3285bf1d4f4fd18a to your computer and use it in GitHub Desktop.
Save alexamies/063ba8e62fc008fa3285bf1d4f4fd18a to your computer and use it in GitHub Desktop.

Revisions

  1. @alexpamies alexpamies created this gist Feb 17, 2020.
    21 changes: 21 additions & 0 deletions pie.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import math
    """Estimates the value of pi
    Uses the secant of successivly smaller slices of a unit circle
    as an estimate of the circumference
    """

    # Initial value is the hypotenuse of a quarter of a circle
    # 2pi ~ 4 * srt(2)
    # pi ~ 2 * sqrt(2)
    # x is the length of the secant
    x = math.sqrt(2)
    N = 2
    print('The estimate of pi is')
    for i in range(30):
    y = math.sqrt(1 - (x / 2)**2)
    z = 1 - y
    x = math.sqrt((x / 2)**2 + z**2)
    N *= 2
    pi = N * x
    print('{0:d}, {1:.20f}'.format(i, pi))