Armstrong Numbers: TIL about integer string conversion limits

Lots of solutions use str to count the number of digits or iterate over them. Mine does not. I was considering moving to str too after the first iteration but it felt weird and I was sure there must be some limit there. After some REPL experimentation … turns out by default, you can only have conversion between str and int for numbers which are at most 4300 base10 digits long!

1 Like

Hi @alech :wave: Welcome to the Exercism Forums!

That’s some nice learning! :smile:

Looks like the limit was introduced in Python 3.10.7, (Python documentation here), due to a DoS security issue (see this SO post for some interesting details). This one in particular:

A Denial Of Service (DoS) issue was identified in CPython because we use binary bignum’s for our int implementation. A huge integer will always consume a near-quadratic amount of CPU time in conversion to or from a base 10 (decimal) string with a large number of digits. No efficient algorithm exists to do otherwise.

You should post your solution here so we can see it! You might also want to consider doing an approaches writeup for the exercise, explaining why you didn’t use string conversion, and why it might be important not to. :wink:

Like the recursion limit, the conversion limit can be raised, but probably shouldn’t be - at least not without a good reason.

1 Like

Here’s another string free version that doesn’t require logarithms: FarbodMoravveji's solution for Armstrong Numbers in Python on Exercism

1 Like

Huh, interesting, thanks for diving deeper here! I wasn’t aware this was due to a DoS issue, despite having done security research about similar issues related to hash tables (including their implementation in Python) in the past :upside_down_face: