Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created April 1, 2012 20:45
Show Gist options
  • Save betawaffle/2278567 to your computer and use it in GitHub Desktop.
Save betawaffle/2278567 to your computer and use it in GitHub Desktop.
def pi k = nil
if k
a = Rational(4, 8 * k + 1)
b = Rational(2, 8 * k + 4)
c = Rational(1, 8 * k + 5)
d = Rational(1, 8 * k + 6)
return (a - b - c - d) * (16 ** -k)
end
return to_enum(:pi) unless block_given?
k = 0
while true
yield pi(k)
k += 1
end
end
@betawaffle
Copy link
Author

pi.reduce(0) do |acc, n|
  sum = acc + n.to_f
  break acc if sum == acc
  sum
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment