Last active
August 29, 2015 14:11
-
-
Save 0V/23d79d0b79dfd24a771c to your computer and use it in GitHub Desktop.
Wallis' Formula by Ruby
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
module MathUtil | |
# | |
# Wallis の公式によって円周率を求めます。 | |
# | |
# [count] | |
# 計算回数を指定します。 | |
# | |
# 使用例 | |
# print(MathUtil.wallis_formula(10000000)) | |
# >> 3.1415925750808533 | |
# print(MathUtil.wallis_formula(1000000000)) | |
# >> 3.141592643066262 | |
def wallis_formula(count) | |
pi = 1.0 | |
1.upto(count){|i| | |
num = 4.0 * i ** 2 | |
pi *= num/(num-1) | |
} | |
return pi * 2 | |
end | |
module_function :wallis_formula | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment