Created
June 19, 2013 10:51
-
-
Save jamierumbelow/5813437 to your computer and use it in GitHub Desktop.
Playing around with calculating π
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
# Using a simple Gregory-Leibniz series we can calculate π | |
# to, theoretically, any degree of accuracy. The series converges very | |
# slowly however, so it'll take 500,000 iterations to get the first five | |
# digits of π. | |
# | |
# This series can be expressed algebraically like so: | |
# | |
# ∞ | |
# Σ (-1)^i * ( 4 / 2i + 1 ) | |
# i=0 | |
# | |
i = 0 | |
pi = 0 | |
while true | |
term = Rational(4, ((2 * i) + 1)) | |
pi += (((-1) ** i) * term) | |
print "π = #{pi.to_f}\r" | |
$stdout.flush | |
i += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment