你会如何写一个非递归算法来计算 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];
}