I am attempting to write a general function that can take any number and back calculate to see if it is the factorial of a smaller number. The easiest and quickest way to do this is to take the initial, larger number and divide by consecutive integers starting at 2
, until the dividend is equal to the divisor (so the quotient is 1
). E.g.
120/2 = 60;
60/3 = 20;
20/4 = 5;
5/5 = 1;
Therefore, 5! = 120
I attempted to use Table
, but then realized Table
will just iterate thru without altering the value of the dividend. I need both my divisor and dividend to change: I need the dividend to take the value of the previous quotient, and I need the divisor to increase by one after each evaluation. I am not really sure how to fix this, here's my most recent attempt:
Clear@inverseFactorial
inverseFactorial[num_Integer, c_] := Block[{list},
list = NestWhileList[(#/c) &, num, {# == c} &];
Length@list+1(*since we started at 2 the length of the list will be one off the actual value to be 'factorialized'*)]
However, it's not working the way I want to, but it's also not showing me any errors when it evaluates. Like most Mathematica users, I do functional programming and these problems which clearly involve some kind of loop are particularly difficult when doing things like this. I'm just interested if there is in fact a way, because this is the way I actually think about the problem.