-
-
Save Robochu/1925166521becabb75756e33f8c3a541 to your computer and use it in GitHub Desktop.
Some ways of calculating pi and e that I came up with. None of these are original; these are just rediscoveries of existing methods.
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
| # Approximate e using bisection. | |
| import math | |
| ITERS = 25 | |
| H = 2 ** (-26) | |
| l = 2 | |
| m = 3 | |
| r = 4 | |
| for i in range(ITERS): | |
| e = m ** H | |
| d = (e - 1 / e) / H / 2 | |
| if d > 1: | |
| r = m | |
| elif d < 1: | |
| l = m | |
| else: | |
| break | |
| m = (l + r) / 2 | |
| print(m, "was calculated by e.py.") | |
| print(math.e, "is the real value of e.") |
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
| # Calculate pi by uniformly placing points inside a square and seeing whether | |
| # they are inside they are inside the corresponding circle. | |
| import math | |
| SIZE = 10000 | |
| SIZE_SQUARED = SIZE * SIZE | |
| x = 0 | |
| y = SIZE | |
| inside = 0 | |
| while y >= 0: | |
| if x * x + y * y < SIZE_SQUARED: | |
| x += 1 | |
| else: | |
| inside += x | |
| y -= 1 | |
| print(inside / SIZE_SQUARED * 4, "was calculated by pi1.py.") | |
| print(math.pi, "is the real value of pi.") |
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
| # Calculate pi by approximating a circle using triangles and applying the half | |
| # angle formula for sine. Susceptible to cancellation error due to subtraction. | |
| import math | |
| # s goes to 0 really fast, so ITERS must be small. | |
| ITERS = 10 | |
| s = 1 | |
| m = 2 | |
| for i in range(ITERS): | |
| s = math.sqrt((1 - math.sqrt(1 - s * s)) / 2) | |
| m *= 2 | |
| print(s * m, "was calculated by pi2.py.") | |
| print(math.pi, "is the real value of pi.") |
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
| # An improvement on pi2.py to help get rid of the cancellation error. | |
| import math | |
| ITERS = 20 | |
| s = 1 | |
| m = 2 | |
| for i in range(ITERS): | |
| s = s / (1 + math.sqrt(1 - s)) / 2 | |
| m *= 2 | |
| print(math.sqrt(s) * m, "was calculated by pi3.py.") | |
| print(math.pi, "is the real value of pi.") |
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
| # Approximate pi using the Taylor Series expansion of sqrt(1 - x ^ 2). | |
| import math | |
| TERMS = 1000 | |
| sum = 5 / 6 | |
| d = 1 | |
| b = 8 | |
| for n in range(3, TERMS): | |
| sum -= d / b / (2 * n - 1) | |
| d *= 2 * n - 3 | |
| b *= 2 * n | |
| print(sum * 4, "was calculated by pi4.py.") | |
| print(math.pi, "is the real value of pi.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment