How would you write a non-recursive algorithm to calculate factorials?

后端 未结 22 963
不思量自难忘°
不思量自难忘° 2020-12-10 11:39

How would you write a non-recursive algorithm to compute n!?

22条回答
  •  伪装坚强ぢ
    2020-12-10 12:08

    Unless you have arbitrary-length integers like in Python, I would store the precomputed values of factorial() in an array of about 20 longs, and use the argument n as the index. The rate of growth of n! is rather high, and computing 20! or 21! you'll get an overflow anyway, even on 64-bit machines.

提交回复
热议问题