gpt4 book ai didi

python - Python 中的长数字散列复杂性

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:12 24 4
gpt4 key购买 nike

Python 如何散列长数字?我猜 32 位整数需要 O(1) 时间,但长整数在 Python 中的工作方式让我认为复杂性不是它们的 O(1)。我已经在相关问题中寻找答案,但没有找到足够直截了当让我有信心的答案。先感谢您。

最佳答案

long_hash() function确实循环并取决于整数的大小,是的:

/* The following loop produces a C unsigned long x such that x is
congruent to the absolute value of v modulo ULONG_MAX. The
resulting x is nonzero if and only if v is. */
while (--i >= 0) {
/* Force a native long #-bits (32 or 64) circular shift */
x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
x += v->ob_digit[i];
/* If the addition above overflowed we compensate by
incrementing. This preserves the value modulo
ULONG_MAX. */
if (x < v->ob_digit[i])
x++;
}

其中 i 是“对象大小”,例如用于表示数字的位数,其中数字的大小取决于您的平台。

关于python - Python 中的长数字散列复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com