Handling very large numbers in Python

I was considering a quick hand assessment in Python. It occurred to me that one way to speed up the process would be to represent all faces of cards and suits as prime numbers and multiply them together to represent hands. For 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 will give each hand a numerical value, which 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 will be divided evenly by 2 ^ 5; any hand with four kings will be evenly divided into 59 ^ 4, etc.

The problem is that a card with seven cards, such as AcAdAhAsKdKhK, has a hash value of about 62.7 quadrillion, which will represent more than 32 bits for presentation within the company. Is there a way to store such large numbers in Python that will allow me to perform arithmetic operations on it?

+72
optimization python largenumber
Feb 11 '09 at 20:13
source share
4 answers

Python supports the integer type "bignum", 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 whatever is more appropriate. In Python 3.0+, the int type is completely disabled.

This is just an implementation detail, though - as long as you have version 2.5 or higher, just perform standard math operations and any number that exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to bignum.

You can find all the gory details in PEP 0237 .

+98
Feb 11 '09 at 20:19
source share
β€” -

python supports arbitrarily large integers :

Example:

β†’> 10 ** 1000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000 000000000000000000000000000000000000000000

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

But still he does not (at the moment) support an arbitrarily large float !!

If you need one big, big, floating, check on the decimal module. There are examples of using these foruns: OverflowError: (34, "Too big result")

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

You can even use the gmpy module if you need acceleration (which may interest you): Processing large numbers in code

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

+29
Jan 07 '14 at 19:50
source share

You could do it for fun, but other than that, it is not a good idea. This will not speed up everything I can come up with.

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

  • Adding cards would be multiplication and removal of division of cards, both large numbers with several words, which are more expensive operations than adding or removing items from lists.

  • The actual numerical value of the hand will not say anything. You will need to define primes and follow the rules of poker in order to compare two hands. h1 <h2 means nothing for such hands.

+23
Feb 11 '09 at 21:07
source share

python supports arbitrarily large integers:

 In [1]: 59**3*61**4*2*3*5*7*3*5*7 Out[1]: 62702371781194950 In [2]: _ % 61**4 Out[2]: 0 
+16
Feb 11 '09 at 20:18
source share



All Articles