我在python中寻找整数的最小值和最大值.例如,在Java中,我们有Integer.MIN_VALUE和Integer.MAX_VALUE.在python中有这样的东西吗?
我有值池,我想通过从某些池中挑选来生成所有可能的无序组合.
例如,我想从池0,池0和池1中选择:
>>> pools = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
>>> part = (0, 0, 1)
>>> list(product(*(pools[i] for i in part)))
[(1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 2), (2, 3, 3), (2, 3, 4), …Run Code Online (Sandbox Code Playgroud) 根据Python整数范围的答案,Python应该具有"任意精度整数" .但是这个结果显然不是任意精度:
$ python -c 'print("%d" % (999999999999999999999999/3))'
333333333333333327740928
Run Code Online (Sandbox Code Playgroud)
根据PEP 237,bignum任意大(不仅仅是C long型的大小).和维基百科说,Python的bignum是任意精度.
那么为什么上面一行代码的结果不正确呢?
为什么二进制搜索的中点算法使用
low + (high-low)/2
Run Code Online (Sandbox Code Playgroud)
而不是
(low + high)/2
Run Code Online (Sandbox Code Playgroud) 我假设这个数字(2^63 - 1)是python可以处理的最大值,或者存储为变量.但这些命令似乎工作正常:
>>> sys.maxsize
9223372036854775807
>>> a sys.maxsize + 1
>>> a
9223372036854775808
Run Code Online (Sandbox Code Playgroud)
那么有什么意义吗?如果计算结果允许,Python可以处理任意大的数字吗?
请注意,这是我的版本的打印输出:
>>> sys.version
3.5.2 |Anaconda custom (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]'
Run Code Online (Sandbox Code Playgroud) 问题 13:http : //projecteuler.net/problem=13
计算出以下一百个 50 位数字之和的前十位数字。那么,问题总和是 5000 位数字,答案是结果中的前 10 位数字吗?
bignumber = list of the 5000 digits
sum(bignumber) = abcdefghijklmnopqrst...
answer = abcdefghj
Run Code Online (Sandbox Code Playgroud)
好吧,当我这样做时sum(bignumber) = 22660(甚至不是 10 位数字)...
我误读了这个问题吗?
def foo():
with open ("bignumber", "r") as myfile:
data=myfile.read().replace('\n', '')
data = map(long, data)
datasum = sum(data)
return (datasum)
Run Code Online (Sandbox Code Playgroud) unsigned long long a,b,c;
cin>>a>>b>>c;
cout<<(a*b*c);
Run Code Online (Sandbox Code Playgroud)
输入:512 294967268 279632277输出:5337484673731225600
但是,当我512 * 294967268 * 279632277使用Python时,我得到的输出是:
42230972821150328832L
Run Code Online (Sandbox Code Playgroud)
为什么我会得到不同的答案?
下面的代码不适用于某些输入。
a, i = set(), 1
while i <= 10000:
a.add(i)
i <<= 1
N = int(input())
if N in a:
print("True")
else:
print("False")
Run Code Online (Sandbox Code Playgroud)
我的想法是,不是通过从1开始乘以2直到超过输入数字来检查每个输入是否为2的幂,而是在每一步进行比较,而是将所有2的幂存储在一组中以便检查O(1)中的给定输入。如何改善呢?