(n==0) 和 (n==1) 作为阶乘函数的终止条件有区别吗?

Is there a difference between (n==0) and (n==1) as a terminating condition for a factorial function?

我写了一个阶乘函数,我用终止条件 (n==0) 和 (n==1) 尝试了 运行 它,但我没有看到任何区别,所以我想知道如果在性能方面有任何差异。

int factorial(int n){
   //If condition is changed to n==1 there is no difference.
   if (n == 0){
      return 1;
   }
   return n  * factorial(n - 1);
}

停止在 n==0 而不是 n==1 会在计算中增加另一个步骤,但这完全可以忽略不计。

主要区别在于数学上的严格性。如果您使用 n==1 终止计算,您的实现将无法计算 0!,根据空积的约定,它被定义为 1