Skip to content

Instantly share code, notes, and snippets.

@alexamies
Created February 17, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexamies/063ba8e62fc008fa3285bf1d4f4fd18a to your computer and use it in GitHub Desktop.
Save alexamies/063ba8e62fc008fa3285bf1d4f4fd18a to your computer and use it in GitHub Desktop.
Estimate Pi
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment