require 'benchmark'
# 階乗計算(再帰)
def fact(n)
return 1 if n == 0
n * fact(n - 1)
end
Benchmark.bm do |x|
x.report { p fact(100) }
end
# 階乗計算(末尾再帰)
def facti(n, a = 1)
return a if n == 0
facti(n - 1, n * a)
end
Benchmark.bm do |x|
x.report { p facti(100) }
end
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
35