Skip to content

Instantly share code, notes, and snippets.

@superscott
Last active January 4, 2016 01:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superscott/8552371 to your computer and use it in GitHub Desktop.
Save superscott/8552371 to your computer and use it in GitHub Desktop.
pi / malloc / ubuntu
#
# ruby pi - how to calculate pi with ruby.
# proving that pi is the limit of this series:
# 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...
#
num = 4.0
pi = 0
plus = true
den = 1
while den < 100000000
if plus
pi = pi + num/den
plus = false
else
pi = pi - num/den
plus = true
end
den = den + 2
end
puts "PI = #{pi}" # calculated value of pi
puts "Math::PI = #{Math::PI}" # pi from the math class
vagrant@precise64:~$ time ruby pi.rb
PI = 3.1415926335902506
Math::PI = 3.141592653589793
real 0m3.368s
user 0m3.352s
sys 0m0.016s
vagrant@precise64:~/ruby-2.1.0$ ./configure LIBS=-ljemalloc
https://www.ruby-forum.com/topic/4418556
http://leonard.io/blog/2012/05/installing-ruby-1-9-3-on-ubuntu-12-04-precise-pengolin/
vagrant@precise64:~$ time ruby pi.rb
PI = 3.1415926335902506
Math::PI = 3.141592653589793
real 0m3.231s
user 0m3.220s
sys 0m0.008s
vagrant@precise64:~$ time ruby pi.rb
PI = 3.14159263359025
Math::PI = 3.14159265358979
real 0m21.406s
user 0m21.397s
sys 0m0.000s
vagrant@precise64:~$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment