Skip to content

Instantly share code, notes, and snippets.

@0V
Last active August 29, 2015 14:11
Show Gist options
  • Save 0V/23d79d0b79dfd24a771c to your computer and use it in GitHub Desktop.
Save 0V/23d79d0b79dfd24a771c to your computer and use it in GitHub Desktop.
Wallis' Formula by Ruby
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