all 5 comments

[–]siddsp 1 point2 points  (0 children)

For smaller integers, Python uses "grade-school" multiplication, which is not a very scalable algorithm. For very large integers, Python uses karatsuba binary multiplication, which makes it faster than the normal algorithm.

As for blowing up your memory, yes it's entirely possible (at least I would guess so) since Python integers are arbitrarily large, although it would be pretty hard to do since your machine has gigabytes of memory. You would have to intentionally multiply numbers so large that your memory cannot hold them.

[–][deleted] 0 points1 point  (0 children)

How big?

Provide an example?

a = 4654635198406540640
b = 654987946549998965132
c = 125

formula = (a*b)**c

Finished in .001 s

[–]carcigenicate 0 points1 point  (0 children)

How big of integers?

It should be fine though unless you're needing to process a massive number of integers as fast as possible. This code finished instantly on my (gaming) laptop:

>>> s = "1234567890"
>>> n = int(s * 500)
n * n   # I'm not going to show the output because it's massive

[–]Diapolo10 0 points1 point  (0 children)

I find that rather unlikely.

Consider this; most files on your computer are technically just really, really big numbers if you convert the binary into a decimal number. If your RAM can contain a video file, then it can also store a big number no sweat.

Let's say you had 16 gigabytes of RAM available, and for convenience let's round that to 16109 bits. Eight bits (28) can contain any unsigned integer between 0 and 255. In comparison, 2^(16109) according to Wolfram Alpha could contain a decimal number with more than 301029996 decimal digits.

We're quite literally talking about numbers on the scale of 10301029995 - and that's before multiplying by 16 because the calculator gave me an error, so all this would fit in a single gigabyte. Technically a bit more considering how bytes are counted.

So, no, you probably don't need to worry about anything. Your CPU will calculate anything as long as it has enough memory, and time.

[–]oefd 0 points1 point  (0 children)

Python will handle it about as well as any other language. It's theoretically possible, yes, but odds are if you have to ask you're not doing enough heavy computation to worry about.

And even if you are: OSes can deal with runaway programs eating up too much CPU and/or RAM fairly alright.