标签归档:largenumber

在Python中处理非常大的数字

问题:在Python中处理非常大的数字

我一直在考虑使用Python快速评估手牌。在我看来,加快处理速度的一种方法是将所有牌面和西服表示为质数,然后将它们相乘以表示手。白衣:

class PokerCard:
    faces = '23456789TJQKA'
    suits = 'cdhs'
    facePrimes = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 53, 59, 61]
    suitPrimes = [2, 3, 5, 7]

    def HashVal(self):
      return PokerCard.facePrimes[self.cardFace] * PokerCard.suitPrimes[self.cardSuit]

这将为每只手提供一个数值,通过模可以告诉我手中有多少个国王或有多少个心。例如,任何有五个或更多球杆的手都会平均除以2 ^ 5;任何有四位国王的手将平均除以59 ^ 4,依此类推。

问题在于,像AcAdAhAsKdKhKs这样的七张牌手的散列值约为62.7万亿次,这需要超过32位才能在内部进行表示。有没有一种方法可以在Python中存储如此大的数字,从而允许我对其执行算术运算?

I’ve been considering fast poker hand evaluation in Python. It occurred to me that one way to speed the process up would be to represent all the card faces and suits as prime numbers and multiply them together to represent the hands. To whit:

class PokerCard:
    faces = '23456789TJQKA'
    suits = 'cdhs'
    facePrimes = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 53, 59, 61]
    suitPrimes = [2, 3, 5, 7]

AND

    def HashVal(self):
      return PokerCard.facePrimes[self.cardFace] * PokerCard.suitPrimes[self.cardSuit]

This would give each hand a numeric value that, through modulo could tell me how many kings are in the hand or how many hearts. For example, any hand with five or more clubs in it would divide evenly by 2^5; any hand with four kings would divide evenly by 59^4, etc.

The problem is that a seven-card hand like AcAdAhAsKdKhKs has a hash value of approximately 62.7 quadrillion, which would take considerably more than 32 bits to represent internally. Is there a way to store such large numbers in Python that will allow me to perform arithmetic operations on it?


回答 0

Python支持“ bignum”整数类型,该整数类型可以处理任意大数。在Python 2.5+中,将调用此类型long并将其与该int类型分开,但是解释器将自动使用更合适的那个类型。在Python 3.0+中,int类型已完全删除。

不过,这只是实现细节-只要您拥有2.5版或更高版本,就只需执行标准数学运算,并且任何超出32位数学界限的数字都将自动(透明地)转换为bignum。

您可以在PEP 0237中找到所有血腥细节。

Python supports a “bignum” integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the int type has been dropped completely.

That’s just an implementation detail, though — as long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.

You can find all the gory details in PEP 0237.


回答 1

python 自然支持任意整数

例:

>>>10 ** 1000 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000百万000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

例如,您甚至可以得到一个巨大的整数值fib(4000000)。

但是它仍然(目前)支持任意大的浮点数

如果您需要一个大的,大的浮子,那么请检查小数模块。在这些论坛上有一些使用示例:OverflowError:(34,“结果太大”)

另一个参考:http : //docs.python.org/2/library/decimal.html

如果需要提高速度,甚至可以使用gmpy模块(这可能是您感兴趣的):在代码中处理大数

另一种参考:https : //code.google.com/p/gmpy/

python supports arbitrarily large integers naturally:

example:

>>> 10**1000
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

You could even get, for example of a huge integer value, fib(4000000).

But still it does not (for now) supports an arbitrarily large float !!

If you need one big, large, float then check up on the decimal Module. There are examples of use on these foruns: OverflowError: (34, ‘Result too large’)

Another reference: http://docs.python.org/2/library/decimal.html

You can even using the gmpy module if you need a speed-up (which is likely to be of your interest): Handling big numbers in code

Another reference: https://code.google.com/p/gmpy/


回答 2

您可以这样做很有趣,但除此之外,这不是一个好主意。它不会加快我能想到的任何速度。

  • 拿到手中的卡将是一个整数分解操作,这比访问数组要昂贵得多。

  • 添加卡将是乘法运算,而删除卡是除法运算,这两个字都是大型的多字数字,这比在列表中添加或删除元素要昂贵得多。

  • 手的实际数字值不会告诉您任何信息。您将需要考虑素数,并遵循扑克规则比较两只手。对于这样的手,h1 <h2毫无意义。

You could do this for the fun of it, but other than that it’s not a good idea. It would not speed up anything I can think of.

  • Getting the cards in a hand will be an integer factoring operation which is much more expensive than just accessing an array.

  • Adding cards would be multiplication, and removing cards division, both of large multi-word numbers, which are more expensive operations than adding or removing elements from lists.

  • The actual numeric value of a hand will tell you nothing. You will need to factor the primes and follow the Poker rules to compare two hands. h1 < h2 for such hands means nothing.


回答 3

python自然支持任意大整数:

In [1]: 59**3*61**4*2*3*5*7*3*5*7
Out[1]: 62702371781194950
In [2]: _ % 61**4
Out[2]: 0

python supports arbitrarily large integers naturally:

In [1]: 59**3*61**4*2*3*5*7*3*5*7
Out[1]: 62702371781194950
In [2]: _ % 61**4
Out[2]: 0

回答 4

python解释器将为您处理它,您只需执行操作(+,-,*,/),它便会正常工作。

int值是无限的。

进行除法时要小心,默认情况下,商会变成float,但float不支持这么大的数。如果收到错误消息说不float支持这么大的数字,则意味着商太大而无法存储在其中float,则必须使用下限除法(//)。

它忽略小数点后的任何小数,这样结果将为int,因此您可以得到大数的结果。

10//3 产出 3

10//4 输出 2

The python interpreter will handle it for you, you just have to do your operations (+, -, *, /), and it will work as normal.

The int value is unlimited.

Careful when doing division, by default the quotient is turned into float, but float does not support such large numbers. If you get an error message saying float does not support such large numbers, then it means the quotient is too large to be stored in float you’ll have to use floor division (//).

It ignores any decimal that comes after the decimal point, this way, the result will be int, so you can have a large number result.

>>>10//3
3

>>>10//4
2