Skip to content

Instantly share code, notes, and snippets.

@dannysmith
Created May 13, 2010 15:37
Show Gist options
  • Save dannysmith/399961 to your computer and use it in GitHub Desktop.
Save dannysmith/399961 to your computer and use it in GitHub Desktop.
include Math
def myPi(reps)
inside = 0
reps.times do
inside += 1 if (Math.sqrt(rand**2+rand**2) < 1)
end
return (4*inside)/reps.to_f
end
puts "Running, please be patient..."
pi_values = []
1000.times {pi_values << myPi(14000)}
f = File.open("pi.txt", "w")
pi_values.each {|i| f.puts i}
f.close
sum = 0
pi_values.each {|i| sum += i}
puts "Success! Data Written to pi.txt..."
puts "Pi is about " + (sum/1000).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment