Skip to content

Instantly share code, notes, and snippets.

@jamierumbelow
Created June 19, 2013 10:51
Show Gist options
  • Save jamierumbelow/5813437 to your computer and use it in GitHub Desktop.
Save jamierumbelow/5813437 to your computer and use it in GitHub Desktop.
Playing around with calculating π
# 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