#This function should return n!
def factorial(n)
return nil if n < 0
n == 0 ? 1 : n*factorial(n-1)
end
Just starting out and this function is blowing my mind I would write this function like this:
def factorial(n)
result = 1
if n == 0
return 1
end
while n > 0
result *= n
n -= 1
end
return result
end
I understande the short hand for the if/else statement. What I dont understand is using n*factorial(n-1) inside the function itself.
It looks like the factorial function is called inside the factorial function, but that can't be whats going on right?