您将如何编写非递归算法来计算阶乘? [英] How would you write a non-recursive algorithm to calculate factorials?

查看:26
本文介绍了您将如何编写非递归算法来计算阶乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何编写非递归算法来计算 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];
}

这篇关于您将如何编写非递归算法来计算阶乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆