你会如何​​写一个非递归算法来计算阶乘?递归、阶乘、你会、算法

2023-09-11 00:06:44 作者:男生英文

你会如何写一个非递归算法来计算 N!

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

推荐答案

由于一个Int32将要溢出的任何大于12!反正,只要做:

Since an Int32 is going to overflow on anything bigger than 12! anyway, just do:

public int factorial(int n) {
  int[] fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 
                362880, 3628800, 39916800, 479001600};
  return fact[n];
}