- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在做一个谜题,我必须处理 10^18 阶的数字。但是,我发现 python 无法在所有领域处理非常大的数字。
具体来说,如果我们分配 a = 1000000000000000000 (10^18) 并进行基本算术计算(+、-、/、*),它会响应。但是,当我在 range() 中使用它时它显示 OverflowError
>>> a = 1000000000000000000
>>> a/2
500000000000000000L
>>> a*2
2000000000000000000L
>>> a+a
2000000000000000000L
>>> a*a
1000000000000000000000000000000000000L
>>> range(a)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
range(a)
OverflowError: range() result has too many items
>>> xrange(a)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
xrange(a)
OverflowError: Python int too large to convert to C long
我使用的是 Python 2.7。
我想在 python 2.7 中使用内置函数来完成它。这不可能吗?
最佳答案
在 Python 2.x 中,range
和 xrange
仅限于使用 C long
并且您的大整数对于它来说太大了.此限制仅仅是由于为 range
和 xrange
做出的实现选择。
在 Python 3.x 中,限制已被移除,您可以使用非常大的整数执行 range()
。
>>> range(2**128)
range(0, 340282366920938463463374607431768211456)
official list of changes for Python 3 是这样说的:
range()
now behaves likexrange()
used to behave, except it works with values of arbitrary size. The latter no longer exists.
在 Python 2.x 中,range()
函数返回一个列表。显然,不可能为范围非常大的所有元素分配内存。 xrange()
函数返回一个 xrange
对象。 documentation 将其描述为“一种不透明的序列类型,它产生与相应列表相同的值,但实际上并没有同时存储它们”。文档接着说:
xrange()
is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long.
这解释了 Python 2.x 中的限制。
我不太确定您可以使用新的 Python 3 支持非常大的范围来做什么。例如,我不建议您尝试这样做:
2**128 in range(2**128)
那会运行很长时间。
您在评论中指出您正在编写代码来统计一个大数。您可以使用如下代码在 Python 中简单地执行此操作:
i = 0
while i<N:
doSomething(i)
i += 1
但是您会发现,如果N
是一个很大的数字,那么这将花费很长时间。根据您的问题,对于订单 218 的 N
值,没有任何解决方法。
关于python - 为什么 python 不能在所有领域处理非常大的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972002/
我在使用 Java 反射获取类中的字段时遇到问题: public class CraftLib { static List alloyRecipes = new ArrayList();
我试图避免此类 ContentDomain 成为上帝类,并将功能隔离到特定类中(以遵循 SRP),就像这样 内容域: public class ContentDomain : IContentDom
1. 什么是领域 百度百科对领域的解释: 领域具体指一种特定的范围或区域 领域一般指的是业务的问题域,领域是有边界的,边界内,规定了我们要做什么,要做的范围,软件项目从开始到交付的过
我有一个包含产品的elasticsearch索引,我试图创建一个具有文本字段功能的搜索列表产品。 数据集的排序示例{"name": "foo", "count": 10}{"name": "bar",
我知道有人问过类似的问题,但我还没有找到明确的解决方案。我正在尝试从一个大类(class)中模拟一个私有(private)领域。私有(private)字段在一些较早的方法中被实例化,我正在尝试对引用该
当使用 JDBC 领域进行授权时,我通常有以下表: 用户表 角色表 分组表 当我现在使用用户名、密码登录时,安全模块会在表中进行查找:为我提供用户的所有角色:用户名。 我可以以某种方式连接到进程并添加
我有两组 Web 应用程序,它们都在同一台 Tomcat 5.5 服务器上运行。 我在 server.xml 中定义了一个通用领域: 我的“美国”应用程序都希望与该数据源共享
我设法使用 key 表在我的 Web 应用程序中启用 SSO。我必须更新以下文件才能使其正常工作: Jass.conf Krb5.conf Server.xml(领域) 网络.xml 它工作正常。我的
我有一个这样定义的结构 private struct Combinators { public const char DirectChild = '>'; public const c
我正在使用 maven 和 eclipse juno 为 Tomcat 7 开发自定义领域。 它看起来很像 Implement a Tomcat Realm with LDAP authenticat
我真的是模拟的新手,正在尝试用模拟对象替换私有(private)字段。目前私有(private)字段的实例是在构造函数中创建的。我的代码看起来像... public class Cache {
在 ECMAScript 规范中引入了“领域”的概念: Before it is evaluated, all ECMAScript code must be associated with a re
我正在为 Subversion 编写一个简单的内部前端。多亏了 WebDAV,我们有一个 Apache 设置为 SVN 存储库提供服务。此外,身份验证是通过 Apache 领域和 Open Direc
有时,C++ 的隐私概念让我感到困惑 :-) class Foo { struct Bar; Bar* p; public: Bar* operator->() const
我现在为此进行了一些搜索,但无法确定 protobuf-net 或 protobuf 通常是否支持以下意义上的前向兼容性: 旧版本的对象使用新字段反序列化新版本的对象,但在将其序列化回时保留该字段,因
根据Nexus 3.x docx,“您还需要启用 Realm 中通常概述的Docker Bearer token Realm 。默认情况下,此 Realm 处于非 Activity 状态” 有人知道如
我正在摆弄 Shiro 安全框架并实现自定义 JDBC 领域。 以下值当前在我的 shiro.ini 文件中设置 jdbcRealm.authenticationQuery = SELECT pass
我有以下 Spring 安全配置类,用于两个独立的安全领域:管理区域和前端区域: @Configuration @EnableWebSecurity @EnableGlobalMethodSecuri
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有 posqtresql 数据库。表中有一个整数字段。如何使它只有积极的?不在 rails 中进行验证。我需要在迁移文件中制作它 最佳答案 您可以在 Postgresql 中使用检查约束。 Rail
我是一名优秀的程序员,十分优秀!