22
22
23
23
24
24
# small helper function for modular exponentiation
25
- def modexpt (base : int , exponent : int , modulo_value : int ) -> int :
26
- """Returns the modular exponentiation, that is the value
25
+ def _modexpt (base : int , exponent : int , modulo_value : int ) -> int :
26
+ """
27
+ Returns the modular exponentiation, that is the value
27
28
of `base ** exponent % modulo_value`, without calculating
28
29
the actual number.
29
- >>> modexpt (2, 4, 10)
30
+ >>> _modexpt (2, 4, 10)
30
31
6
31
- >>> modexpt (2, 1024, 100)
32
+ >>> _modexpt (2, 1024, 100)
32
33
16
33
- >>> modexpt (13, 65535, 7)
34
+ >>> _modexpt (13, 65535, 7)
34
35
6
35
36
"""
36
37
37
38
if exponent == 1 :
38
39
return base
39
40
if exponent % 2 == 0 :
40
- x = modexpt (base , exponent / 2 , modulo_value ) % modulo_value
41
+ x = _modexpt (base , exponent / 2 , modulo_value ) % modulo_value
41
42
return (x * x ) % modulo_value
42
43
else :
43
- return (base * modexpt (base , exponent - 1 , modulo_value )) % modulo_value
44
+ return (base * _modexpt (base , exponent - 1 , modulo_value )) % modulo_value
44
45
45
46
46
47
def solution (base : int = 1777 , height : int = 1855 , digits : int = 8 ) -> int :
47
- """Returns the last 8 digits of the hyperexponentiation of base by
48
+ """
49
+ Returns the last 8 digits of the hyperexponentiation of base by
48
50
height, i.e. the number base↑↑height:
49
51
50
52
>>> solution()
@@ -59,10 +61,11 @@ def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int:
59
61
# last 8 digits
60
62
modulo_value = 10 ** digits
61
63
62
- # calculate base↑↑height (mod modulo_value)
64
+ # calculate base↑↑height (mod modulo_value) by repeated modular
65
+ # exponentiation 'height' times
63
66
result = base
64
67
for i in range (1 , height ):
65
- result = modexpt (base , result , modulo_value )
68
+ result = _modexpt (base , result , modulo_value )
66
69
67
70
return result
68
71
0 commit comments