A place for my personal musings, about programming, design or whatever come across my mind

failing factorial

Hours ago I got a programming test question about factorial, which I didn’t do really well, only completed with the help of the interviewer. Damn.
The worst thing is that I have done this question before, and I know in the head I can use recursion for it, but I totally go blank on how to do it with recursion. Here go my chance…sigh…

Now, here is how factorial can be done.
With Recursion.

def fact n
  if n == 0
    1
  else
    n = n * fact(n-1)
  end
end

That simple.

Factorial without recursion, using inject instead

def fact n
  if n == 0
    1
  else
    (1..n).inject { |a,b| a*b } 
  end
end

Isn’t that difficult, huh? What was I thinking?!

Leave A Comment