importmathclassBigInt:"""BigInt gives integer approximation and easy to read strings"""_first=""_last=""_length=None# BigInt
val:int=None# actual int, if applicable
def__init__(self,val:int=0)->None:"""BigInt gives integer approximation and easy to read strings
Args:
val (int | BigInt, optional): Defaults to 0.
"""self.set(val)defset(self,val:int):"""Update large integer with int or BigInt
Args:
val (int | BigInt): integer to update
Returns:
BigInt: returns self
"""ifisinstance(val,BigInt):self.val=val.valifval.valisNone:self._first=val._firstself._last=val._lastself._length=val._lengthreturnelse:self.val=valtry:float(self.val)exceptOverflowError:self._first=self.firstself._last=self.lastself._length=Noneself.val=Nonereturnself@propertydeffirst(self)->str:"""
Returns:
str: initial part of BigInt
"""ifself._first:returnself._firstifnotself.val:return""returnstr(self.val)[:8]@propertydeflast(self)->str:"""
Returns:
str: ending part of BigInt.
"""ifself._last:returnself._lastifnotself.val:return""returnstr(self.val)[-8:]@propertydeflength(self):"""
Returns:
BigInt | None: length of BigInt, as another BigInt, if applicable
"""ifself._length:returnself._lengthifnotself.val:returnNonereturnBigInt(len(str(self.val)))def__repr__(self)->str:ifnotself.valorself.val>10**16:s=""ifself.last:s=f"{self.first}...{self.last}"e=""ifself.length:m=""ifself.first:m=self.first[0]iflen(self.first)>1:m+="."+self.first[1:]e=f"{m}e+{self.length}"ifs:ife:returnf"{s} ({e})"returnsreturnereturnf"{self.val}"classTower(BigInt):"""Exponent tower, of 3 by default"""k:intbase:intdef__init__(self,k:int,base:int=3)->None:"""Exponent tower, of 3 by default
Args:
k (int): Number of bases in the tower
base (int, optional): Defaults to 3.
"""self.k=kself.base=basesuper().__init__(base)prev=BigInt(1)defup_pow(m:int)->None:out=BigInt()ifself.val:try:float(base)**self.valself.set(base**self.val)returnexceptOverflowError:passlog=self.val*math.log10(base)fl=math.floor(log)r=log-flout._first=f"{(10**r):.7f}".replace(".","")ifr>0else""out._length=BigInt(fl)elifprev.val:loglog=prev.val*math.log10(base)+math.log10(math.log10(base))fl=math.floor(loglog)r=loglog-fllength=BigInt()length._first=f"{(10**r):.7f}".replace(".","")ifr>0else""length._length=BigInt(fl)out._length=lengthout._last=tetmod_digits(int(BigInt(base).last),m)out.val=Noneself.set(out)forminrange(2,k+1):next_prev=BigInt(self)up_pow(m)prev=next_prevdefmodpow(base:int,exp:int,mod:int)->int:"""Modular exponentiation
Adapted from https://en.wikipedia.org/wiki/Modular_exponentiation#Implementation_in_Lua
```
py
(base ** exp) % mod
```
Args:
base (int):
exp (int):
mod (int):
Returns:
int:
"""r=1base=base%modwhileexp:ifexp%2:r=(r*base)%modexp=exp//2base=(base**2)%modreturnrdefmodpow_digits(base:int,exp:int,digits:int=8)->str:"""Last digits of exponentiation
```
py
(base ** exp) % (10 ** digits) # With frontmost "0" padding
```
Args:
base (int):
exp (int):
digits (int, optional): Defaults to 8.
Returns:
str: string of digits, representing n last digits
"""mod=10**digitsreturnstr(modpow(base,exp,mod)).rjust(digits,"0")deftetmod(base:int,k:int,mod:int)->int:"""Tetration with modulo
Adapted from https://math.stackexchange.com/a/4225702/958918
```
py
(base ↑↑↑ k) % mod
```
Args:
base (int):
k (int):
mod (int):
Returns:
int:
"""ifk==1:returnbase%modelifk==2:returnmodpow(base,base,mod)phi=euler_phi(mod)returnmodpow(base,phi+tetmod(base,k-1,phi)%phi,mod)deftetmod_digits(base:int,k:int,digits:int=8)->str:"""Last digits of tetration
```
py
(base ↑↑↑ k) % (10 ** digits) # With frontmost "0" padding
```
Args:
base (int):
k (int):
digits (int, optional): Defaults to 8.
Returns:
str: string of digits, representing n last digits
"""mod=10**digitsreturnstr(tetmod(base,k,mod)).rjust(digits,"0")defeuler_phi(n:int)->int:"""Euler's Phi
From https://www.geeksforgeeks.org/eulers-totient-function/
Args:
n (int):
Returns:
int: the count (number) of coprimes less than n
"""# Initialize result as n
result=n# Consider all prime factors
# of n and subtract their
# multiples from result
p=2whilep*p<=n:# Check if p is a
# prime factor.
ifn%p==0:# If yes, then
# update n and result
whilen%p==0:n=int(n/p)result-=int(result/p)p+=1# If n has a prime factor
# greater than sqrt(n)
# (There can be at-most
# one such prime factor)
ifn>1:result-=int(result/n)returnresultif__name__=="__main__":forninrange(2,6+1):print("-",n,Tower(n))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I made it, but I could get the first digits right (according to WolframAlpha).