-1

I wanted to calculate a very large number and since python supports arbitraryly large numbers I thought that's perfect.

So, here it is:

import math
x=2**24
y=3840*2160
z=x**y
print("z is calculated")
print(z)

Well, the last I see is "z is calculated", so it is not the calculation itself that is the problem.

But even after an hour I do not see any other output.

So can someone explain what is going on here?

PS: z has about 60 Million digits...

U. W.
  • 414
  • 2
  • 10
  • and what are you going to do after printing out that 60 million digit number? check them all? – Mitch Wheat May 27 '20 at 07:57
  • The number is atleast 25GB. Why on earth do you want to print it? – Onyambu May 27 '20 at 08:03
  • The number has about 60 Million digits, so it should be 60 MB of output. – U. W. May 27 '20 at 08:16
  • I don't really want to *do* anything with this number, I justed wanted to calculate it for fun and ran into this performance problem. – U. W. May 27 '20 at 09:07
  • 1
    @U.W.: That's like asking a guy to ride a bicycle from South Africa to Sweden and then saying it's a "performance problem" when the poor dude doesn't arrive within 10 minutes. To print a number like that you have to find the remainder of dividing by 10 for each digit, and even with a highly optimized mixture of C and assembly using a "multiply by pre-computed reciprocal" approach (a motorbike instead of your python bicycle) it's still going to take an extremely long time. – Brendan May 27 '20 at 10:06
  • Well, I am still surprised, because calculating this large number finishes rather fast and then printing it takes forever on a contemporary computer. Even if you have to divide by 10 sixty million times, that should still not take more than an hour on a 4 GHz machine. And printing not quite so large numbers (like 30000 digits) goes reasonably fast, btw... – U. W. May 27 '20 at 10:47

1 Answers1

0

So, this is expected behaviour and "won't fix".

The algorithm converting an integer to a string has quadratic complexity and an improvement is too much effort since it happens so rarely.

As a workaround it was recommended that I use a package like GMP, which seems to be better suited.

I just hope that this does not open a door to DOS attacks to servers where the attackers can provide such a number as input. it is enough if the server wants to log that number to freeze up.

U. W.
  • 414
  • 2
  • 10