Created
February 17, 2020 18:57
-
-
Save alexamies/063ba8e62fc008fa3285bf1d4f4fd18a to your computer and use it in GitHub Desktop.
Estimate Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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