At first glance, it seems to be very straight forward. However, when you look at the sample calculation from Wolfram Alpha.
https://www.wolframalpha.com/input/?i=3%5E3%5E3%5E3
This breaks both long long
(integer, 18-19 digits) and double
(float / IEEE 754, up to e+308 digits, with 17 digits' precision).
However, I can cheat a little with Python, as it will automatically allocate more bytes for integer.
Still, 3^(7.625e+13) takes abnormally very long time... (3^3^3 = 7.625e+13).
class Int: MAX_LEN = 10 def __init__(self, val: int) -> None: if isinstance(val, Int): self.val = val.val else: self.val = val def exp3(self): return Int(3 ** self.val) def tetrate3(self, n: int): first = Int(self) for _ in range(n - 1): first = first.exp3() return first def __repr__(self) -> str: s = str(self.val) if len(s) > self.MAX_LEN: h = int(self.MAX_LEN / 2) return f"{s[:h]}...{s[-h:]} ({s[0]}.{s[1:4]}e+{Int(len(s))})" return s
Top comments (3)
I made it, but I could get the first digits right (according to WolframAlpha).
You could see this StackOverflow answer : math.stackexchange.com/a/176257
That being said, I suppose that 3^3^3^3 will use about 3639GB memory to store. (if I've got my maths right)
Edit: I certainly got my mathematics wrong I suppose :(
I tried, but not to the end, with exponentiation by squaring.
I stopped at
And I cached the file storing
6846170 + 20538507
digits, and it takes 27 MBBTW, the expected result is
I may get some of the first digits wrong, though.