Ways to calc factorial with ruby
Days ago,i got a problem about how to calc factorial
Way 1 — the original way
def func(m)
s = 1
(1..m).each do |n|
s *= n
end
s
end
Way 2 — less way
def func(m)
return 1 if m == 1
m*func(m-1)
end
Way 3 — ruby way
def func1(m)
(1..m).inject {|s,i| s *= i}
end
Or
def func2(m)
(1..m).inject :*
end