Skip to content

Commit c8747e4

Browse files
committed
Update docstrings and mark helper function private
1 parent bee3abf commit c8747e4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

project_euler/problem_188/sol1.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,31 @@
2222

2323

2424
# 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
2728
of `base ** exponent % modulo_value`, without calculating
2829
the actual number.
29-
>>> modexpt(2, 4, 10)
30+
>>> _modexpt(2, 4, 10)
3031
6
31-
>>> modexpt(2, 1024, 100)
32+
>>> _modexpt(2, 1024, 100)
3233
16
33-
>>> modexpt(13, 65535, 7)
34+
>>> _modexpt(13, 65535, 7)
3435
6
3536
"""
3637

3738
if exponent == 1:
3839
return base
3940
if exponent % 2 == 0:
40-
x = modexpt(base, exponent / 2, modulo_value) % modulo_value
41+
x = _modexpt(base, exponent / 2, modulo_value) % modulo_value
4142
return (x * x) % modulo_value
4243
else:
43-
return (base * modexpt(base, exponent - 1, modulo_value)) % modulo_value
44+
return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value
4445

4546

4647
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
4850
height, i.e. the number base↑↑height:
4951
5052
>>> solution()
@@ -59,10 +61,11 @@ def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int:
5961
# last 8 digits
6062
modulo_value = 10 ** digits
6163

62-
# calculate base↑↑height (mod modulo_value)
64+
# calculate base↑↑height (mod modulo_value) by repeated modular
65+
# exponentiation 'height' times
6366
result = base
6467
for i in range(1, height):
65-
result = modexpt(base, result, modulo_value)
68+
result = _modexpt(base, result, modulo_value)
6669

6770
return result
6871

0 commit comments

Comments
 (0)