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

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

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

22条回答
  •  孤城傲影
    2020-12-10 12:32

    long fact(int n) {
        long x = 1;
        for(int i = 1; i <= n; i++) {
            x *= i;
        }
        return x;
    }
    

提交回复
热议问题