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

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

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

22条回答
  •  醉梦人生
    2020-12-10 12:20

    public int factorialNonRecurse(int n) {
        int product = 1;
    
        for (int i = 2; i <= n; i++) {
            product *= i;
        }
    
        return product;
    }
    

提交回复
热议问题