-2

As stated here it is quadratic, but why?

AmirHossein
  • 1,310
  • 1
  • 12
  • 19

1 Answers1

5

I think the quadratic part is reading the integer from text. The standard algorithm looks like this:

v = 0
for each digit:
    v = v * 10 + digit

It looks like this is just O(n) on the number of digits, but if you are working with arbitrary precision integers like this problem then the multiplication by 10 is also O(n), making the whole thing O(n^2).

Paul Johnson
  • 17,438
  • 3
  • 42
  • 59