- Set
X=1
.
- Generate
F=X!
- Is F = the input? If yes, then
X
is N.
- If not, then set
X=X+1
, then start again at #2.
You can optimize by using the previous result of F
to compute the new F
(new F = new X * old F
).
It's just as fast as going the opposite direction, if not faster, given that division generally takes longer than multiplication. A given factorial A!
is guaranteed to have all integers less than A
as factors in addition to A, so you'd spend just as much time factoring those out as you would just computing a running factorial.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…