Skip to content

Instantly share code, notes, and snippets.

@safiire
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safiire/8893709 to your computer and use it in GitHub Desktop.
Save safiire/8893709 to your computer and use it in GitHub Desktop.
Compute π with an infinite series
def f(n)
(2 * n + 1)**-1
end
def g(n)
((-1)**n).to_f
end
def pi
0.upto(100000).inject(0) do |sum, n|
sum + f(n) * g(n)
end * 4
end
p 0.upto(10).map{|n| f(n) }
p 0.upto(10).map{|n| g(n) }
puts pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment