February 11th, 2005, 09:00 PM
very large integer
I am messing around with something and i wanted to see how big of a number python could handle i thought it would be the same as what unsigned long was in c but it seems to be even larger
here is what i did
i couldn't get it to print x but it seems to be abel to do the math is this right or is there just an error that is making it seem liek it does the math?
x = 10 ** 10000000
if x mod 2:
February 12th, 2005, 01:03 PM
I don't know if thereís a defined limit to how big Pythons long type can be, however I would assume it would be the same as in C - not sure what that is . The problem might end up being speed. Everything goes fine up to10**100000, after that it just takes way too long to get the answer.
Edit: Itís been 10 minutes now and Iím still waiting .
Anyway, if you run the code above it will hand for a while, and eventually give you a SyntaxError since the name Ďmodí isnít a valid operator: try the modulus operator - %. After this you should be able to print the value of x however the number will be a little big to fit on the screen. You might want to write to a file instead .
Hope this helps,
Last edited by netytan; February 12th, 2005 at 01:12 PM.
February 12th, 2005, 02:48 PM
lol ya sorry not mod, i ment % sorry i was thinking vb
anyway, it should just print 0 since 2 goes into that number evenly. I know it does, but i was just wondering if it actually was workign.
also it doesn't write it to a file i tried that second. i even tryed using cpickle to do it and it didn't
February 12th, 2005, 03:09 PM
You could do something like:
You can probably guess what this does but just in case: it will print 0 if the number divides equally in half or 1 if it doesn't.
print 10 ** 10000000 % 2
Just a shortcut ,
February 12th, 2005, 03:14 PM
ya that is what i ment.
So you think it is really workign with that large number?
but c long isn't close to 10 ** 10000000
February 12th, 2005, 03:17 PM
Python long types are arbitrary precision ints - they do not use a fixed number of bytes, and are only limited by the amount of memory your computer has. You could conceivably have a long int that was millions of bits long. Doing maths with numbers that big would be rather slow, though.
Here is a smaller example: 2 to the power 8000 - i.e. a number that needs 1000 bytes to store it:
Dave - The Developers' Coach
>>> 1L << 8000
February 12th, 2005, 03:27 PM
Well, the CPU usage jumps to 100% when I run the line and stays there so I'd guess it's working it out; I just dont want to wait hours for it to print something .
I'm sure there must be a better way to find the maximum size of a Long in Python.
Edit: Like Dave . Nice reply!
Last edited by netytan; February 12th, 2005 at 03:33 PM.
February 12th, 2005, 05:29 PM
Incidentally, since 10 is about 3.3 bits, 10**10,000,000 requires over 33 million bits to represent it, or over 4 million bytes. It is not surprising it takes a while to calculate.
Dave - The Developers' Coach
February 12th, 2005, 09:38 PM
how does python make it in c so it can actually handle that num?
February 14th, 2005, 07:54 PM
When calculating, my CPU usuage stays at about 33% for 5 minutes, while it was calculating a very large number.
I'm using an Intel P4 Prescott @ 3.2 GHz, and clue why this is?
February 15th, 2005, 04:24 PM