File tree 2 files changed +53
-0
lines changed
project_euler/problem_188 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ The hyperexponentiation of a number
3
+ Problem 188
4
+
5
+ The hyperexponentiation or tetration of a number a by a positive integer b,
6
+ denoted by a↑↑b or ba, is recursively defined by:
7
+
8
+ a↑↑1 = a,
9
+ a↑↑(k+1) = a(a↑↑k).
10
+
11
+ Thus we have e.g. 3↑↑2 = 3^3 = 27, hence 3↑↑3 = 3^27 = 7625597484987 and
12
+ 3↑↑4 is roughly 103.6383346400240996*10^12.
13
+
14
+ Find the last 8 digits of 1777↑↑1855.
15
+ """
16
+
17
+
18
+ def solution (a = 1777 , k = 1855 , digits = 8 ):
19
+ """Returns the last 8 digits of the hyperexponentiation of a by k.
20
+
21
+ >>> solution()
22
+ 95962097
23
+ >>> solution(3, 2)
24
+ 27
25
+ >>> solution(3, 3)
26
+ 97484987
27
+ """
28
+
29
+ # we calculate everything modulo 10^8, since we only care about the
30
+ # last 8 digits
31
+ modulo_value = 10 ** digits
32
+
33
+ # small helper function for modular exponentiation, to keep the result
34
+ # values small enough
35
+ def modexpt (base , exponent ):
36
+ if exponent == 1 :
37
+ return base
38
+ if exponent % 2 == 0 :
39
+ x = modexpt (base , exponent / 2 ) % modulo_value
40
+ return (x * x ) % modulo_value
41
+ else :
42
+ return (base * modexpt (base , exponent - 1 )) % modulo_value
43
+
44
+ # calculate a ↑↑ k (mod modulo_value)
45
+ result = a
46
+ for i in range (1 , k ):
47
+ result = modexpt (a , result ) % modulo_value
48
+
49
+ return result
50
+
51
+
52
+ if __name__ == "__main__" :
53
+ print (solution ())
You can’t perform that action at this time.
0 commit comments