【问题标题】:Store Factorial of large Numbers in python array在python数组中存储大数的阶乘
【发布时间】:2022-01-18 09:34:20
【问题描述】:

我想存储 1 到 200,000 范围内的数字的阶乘,但由于较大数字的阶乘非常大,我的程序空间不足。有什么办法可以将它们存储在数组中?

我的代码:

factorial_of_numbers = []
factorial_of_numbers = [factorial_of_numbers[-1] for x in range(0, 200001) if not factorial_of_numbers.append(x*factorial_of_numbers[-1] if factorial_of_numbers else 1)]

使用这种方法,我只能将阶乘存储到 10,000。

谁能建议任何其他方法或如何存储大阶乘?

【问题讨论】:

  • 如果你解释你需要阶乘的目的,有人可能会看到另一种方法。
  • 这是一个列表,不是数组
  • 听起来你真的需要一个基本数论的教程,而不是打败你的电脑
  • 如果你真的想这样做,最好考虑将它们推送到文件而不是使用堆。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python arrays math list-comprehension factorial


【解决方案1】:

这只花了我大约一分钟的时间运行:

>>> factorials = [1]
>>> for _ in range(200000):
...     factorials.append(factorials[-1] * (len(factorials) + 1))
...
>>> factorials[-1]
2840464893194083513077942336...(VERY LONG NUMBER)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多