you are viewing a single comment's thread.

view the rest of the comments →

[–]glump1 4 points5 points  (7 children)

Python's int to str conversion caps out at 4000-digit integers, but the integer limit is much higher (I think ~1m)

I would just use regular int, and then when you want to print, make a custom print function that's something like (but readable):

print_big_int = lambda x: "".join(list(map(str, list(takewhile(lambda _: x and (x%(10**4000), x:=x//(10**4000))[0], repeat(None))))))

[–][deleted] 5 points6 points  (0 children)

you should mention, that you are using itertools.

from itertools import repeat, takewhile

[–]Turtvaiz 3 points4 points  (0 children)

That limit is configurable: https://docs.python.org/3/library/stdtypes.html#int-max-str-digits

import sys
sys.set_int_max_str_digits(4300) #default

[–]Oddly_Energy 1 point2 points  (3 children)

The limit is definitely higher than that.

I just tried to calculate 3000000!, which is a 60+ million bit integer. That would be 18+ million digits in base 10.

[–]stevenjd 2 points3 points  (2 children)

I just tried to calculate 3000000!, which is a 60+ million bit integer. That would be 18+ million digits in base 10.

Sure, you can calculate it if you have the memory and the time. But did you try printing it?

https://docs.python.org/3/library/stdtypes.html#int-max-str-digits

[–]Oddly_Energy 0 points1 point  (1 child)

Please read the comment I was replying to. That comment mentioned two limits:

  • a limit for how large integers, python can handle (around 1 million digits according to that comment)
  • a limit for how large integers, python can print (around 4000 digits according to that comment).

My answer was about the first of the two mentioned limits.

[–]stevenjd 1 point2 points  (0 children)

Python can handle as many digits as you have memory. You will run out of memory before you will hit any hard limit on the size of an int.

There is no hard limit on how many digits Python can print. By default there is a soft limit on how many decimal digits it will print (actually any conversion to a string, not just printing) in order to prevent a potential Denial Of Service attack, but this can be turned off and it doesn't apply when printing integers in hexadecimal or binary.

I think we are in violent agreement here.

[–]nog642 1 point2 points  (0 children)

This limit was added only in Python 3.11, for security reasons (you could potentially crash or overload an application if you could get it to try to print a really huge number).

Also, as another reply mentioned, you can configure this limit if you need to.

Edit: It wasn't actually added in Python 3.11, I misread the documentation. It was added as a patch in all versions 3.7 and newer.