Implementing a factorial algorithm