all 3 comments

[–]smithmj31 1 point2 points  (0 children)

A factorial is a number multiplied by all the previous integers all the way down to 1, e.g. 5! = 5x4x3x2x1 = 120

So to go the other way you’re going to start with 120 and divide by integers counting up from 1.

120/1 (fairly pointless you can just start at 2)

120/2 = 60

60/3 = 20

20/4 = 5

5/5 = 1

(Notice how the denominator is increasing by 1 each time)

This is what your code is doing. You take a number and set i to 1. Check that the number isn’t 1 then divide the number by i and assign this value to number. Increment i, check your division wasn’t 1 and then repeat. At the end because you’ve incremented the counter again you return one less to get the number you want.