Guest User

Untitled

a guest
May 20th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. #
  2. # ruby pi - how to calculate pi with ruby.
  3. # proving that pi is the limit of this series:
  4. # 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...
  5. #
  6. num = 4.0
  7. pi = 0
  8. plus = true
  9.  
  10. den = 1
  11. while den < 10000000
  12. if plus
  13. pi = pi + num/den
  14. plus = false
  15. else
  16. pi = pi - num/den
  17. plus = true
  18. end
  19. den = den + 2
  20. end
  21.  
  22. puts "PI = #{pi}" # calculated value of pi
  23. puts "Math::PI = #{Math::PI}" # pi from the math class
Add Comment
Please, Sign In to add comment