【问题标题】:Dividing long ints in Python returns wrong answer在 Python 中除以长整数返回错误的答案
【发布时间】:2012-03-22 11:01:34
【问题描述】:

在 Python 中,以下代码

from math import factorial
print factorial(40)/(factorial(20)*factorial(20))

打印出 136772786996。

WolframAlpha 说答案是错误的;它应该是137846528820。 这里有什么问题,我该如何解决?

【问题讨论】:

  • 嗯,我得到了 Wolfram 的答案。 factorial(40)factorial(20) 得到什么?那些与 Wolfram 的阵容如何?
  • 今天有人在投影仪上吗?
  • 你用的是什么版本的 Python?
  • 报告的错误(我也没看出问题)是1073741824,也就是2^30。这似乎.. 可疑,因为 30 通常是 Python 整数存储中的一个幻数(#if PYLONG_BITS_IN_DIGIT == 30 等)我可以确信这里对于某些架构/编译器/构建选项/阶段存在真正的问题-the-moon 配置。
  • 噢,伙计。您必须先说“我正在通过基于 Web 的解释器运行代码”之类的话。我爱上了我的想法,即在某个不起眼的平台上这是一个奇怪的编译器错误,现在它可能只是一些界面错误。 :-(

标签: python math int long-integer factorial


【解决方案1】:

对我来说很好用:

>>> from math import factorial
>>> print factorial(40)/(factorial(20)*factorial(20))
137846528820

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多