Created
March 17, 2014 04:28
-
-
Save mikrofusion/9593990 to your computer and use it in GitHub Desktop.
calculate 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
#!/usr/bin/ruby | |
def pi_recursive(depth, count, i) | |
if count >= depth then return 0 end | |
r = 4.0 / (i..(i + 2)).to_a.inject(1) { |sum, x| sum * x } | |
pi_recursive(depth, count + 1, i + 2) + | |
if count % 2 == 1 then + r else - r end | |
end | |
def pi(depth) # using Nilakantha series | |
pi_recursive(depth, 1, 2) + 3 | |
end | |
puts "#{pi(1000)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment