Reverse factorial

前端 未结 17 1462
南笙
南笙 2020-12-05 03:12

Well, we all know that if N is given it\'s easy to calculate N!. But what about the inverse?

N! is given and you are about to find N - Is that possible ? I\'m curio

17条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 03:50

    1. Set X=1.
    2. Generate F=X!
    3. Is F = the input? If yes, then X is N.
    4. 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.

提交回复
热议问题