Created
June 1, 2016 22:10
-
-
Save charlesreid1/80338d9344e71490201b212f39632726 to your computer and use it in GitHub Desktop.
Math 163 - Calculating Infinity Project
This file contains hidden or 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 | |
print("\n\n") | |
print("Welcome to Dr. Reid's Magical Approximation Program! \n\n") | |
print("This program computes approximations of Pi. \n\n") | |
print("Sources for formulas/approximations:") | |
print(" * http://mathworld.wolfram.com/PiApproximations.html") | |
print(" * http://mathworld.wolfram.com/PiFormulas.html\n\n") | |
# ====================== | |
# Pi Approximations | |
# -------------------- | |
# Archimedes | |
pi_approx = 22/7 | |
print('Archimedes\' Approximation:') | |
print('Computed Value of Pi: %.16F'%(pi_approx)) | |
print('Error: %.2E'%( math.fabs(math.pi - pi_approx)/math.pi )) | |
print("\n") | |
# -------------------- | |
# de Jerphanion | |
pi_approx = math.log(10691/462) | |
print('de Jerphanion\'s Approximation:') | |
print('Computed Value of Pi: %.16F'%(pi_approx)) | |
print('Error: %.2E'%( math.fabs(math.pi - pi_approx)/math.pi )) | |
print("\n") | |
# ======================= | |
# Pi Formulas | |
# -------------------- | |
# Ramanujan's Method | |
# This expression converges extremely quickly - 8 decimal places for each term. | |
Nterms = 1 | |
one_over_pi = 0 | |
a = ((2*math.sqrt(2))/(9801)) | |
for k in range(Nterms): | |
# Be careful selecting a value of k, these numbers will blow up fast | |
num = math.factorial(4*k)*(1103+26390*k) | |
denom = math.pow(math.factorial(k),4) * math.pow(396,4*k) | |
one_over_pi += a*(num/denom) | |
pi_approx = 1/one_over_pi | |
print('Ramanujan\'s Formula:') | |
print('Computed Value of Pi: %.16F'%(pi_approx)) | |
print('Error: %.2E'%( math.fabs(math.pi - pi_approx)/math.pi )) | |
print("\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment