If integer precision is not of utmost importance, you could use float
numbers
>>> 3**3**3
7625597484987
>>> 3.**3.**3.
7625597484987.0
However, for larger values those will quickly reach their limits:
>>> 5.**5.**5.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Numerical result out of range')
You can get much higher with decimal
:
>>> import decimal
>>> d = decimal.Decimal
>>> d(5)**d(5)**d(5)
Decimal('1.911012597945477520356404560E+2184')
>>> d(10)**d(10)**d(8)
Decimal('1.000000000000000000000000000E+100000000')
By default, even those can not represent 10**10**10
:
>>> d(10)**d(10)**d(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/decimal.py", line 2386, in __pow__
ans = ans._fix(context)
File "/usr/lib/python2.7/decimal.py", line 1676, in _fix
ans = context._raise_error(Overflow, 'above Emax', self._sign)
File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.Overflow: above Emax
But those limits are not fixed. Using getcontext()
you can make them as big as you want:
>>> decimal.getcontext().Emax = 1000000000000
>>> d(10)**d(10)**d(10)
Decimal('1.000000000000000000000000000E+10000000000')
But remember that those numbers are not 100% precise to the last digit (your computer probably does not even have enough memory to store each digit), so don't be surprised if this happens:
>>> d(10)**d(10)**d(10) == d(10)**d(10)**d(10) + 1000000
True