0
how to find factorial using def?
7 Answers
+ 10
def factorial(n)
(1..n).inject(:*) || 1
end
puts factorial(50)
+ 8
@Mickel thnx I like Ruby so much so just made like that šš
+ 8
def fact(n) (1..n).inject(1) {|r,i| r*i }
end
fact(6) => 720
@Bool you can do like this too šš
+ 5
thanku Gwen sorry for being so straight
+ 4
šš
+ 3
@Gawen I've always liked that way of writing the factorial in Ruby
+ 3
yeh @gwen is right, you can check it here
same thing is covered here what gwen said and not just that but you can find more simple way
thanku
https://www.ruby-forum.com/topic/105085