mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
4d0c830d2c
* ci(pre-commit): Add ``flake8-builtins`` additional dependency to ``pre-commit`` (#7104) * refactor: Fix ``flake8-builtins`` (#7104) * fix(lru_cache): Fix naming conventions in docstrings (#7104) * ci(pre-commit): Order additional dependencies alphabetically (#7104) * fix(lfu_cache): Correct function name in docstring (#7104) * Update strings/snake_case_to_camel_pascal_case.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/stacks/next_greater_element.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/index_calculation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update graphs/prim.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update hashes/djb2.py Co-authored-by: Christian Clauss <cclauss@me.com> * refactor: Rename `_builtin` to `builtin_` ( #7104) * fix: Rename all instances (#7104) * refactor: Update variable names (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: Create ``tox.ini`` and ignore ``A003`` (#7123) * revert: Remove function name changes (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename tox.ini to .flake8 * Update data_structures/heap/heap.py Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> * refactor: Rename `next_` to `next_item` (#7104) * ci(pre-commit): Add `flake8` plugin `flake8-bugbear` (#7127) * refactor: Follow `flake8-bugbear` plugin (#7127) * fix: Correct `knapsack` code (#7127) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
"""
|
|
Project Euler Problem 188: https://projecteuler.net/problem=188
|
|
|
|
The hyperexponentiation of a number
|
|
|
|
The hyperexponentiation or tetration of a number a by a positive integer b,
|
|
denoted by a↑↑b or b^a, is recursively defined by:
|
|
|
|
a↑↑1 = a,
|
|
a↑↑(k+1) = a(a↑↑k).
|
|
|
|
Thus we have e.g. 3↑↑2 = 3^3 = 27, hence 3↑↑3 = 3^27 = 7625597484987 and
|
|
3↑↑4 is roughly 103.6383346400240996*10^12.
|
|
|
|
Find the last 8 digits of 1777↑↑1855.
|
|
|
|
References:
|
|
- https://en.wikipedia.org/wiki/Tetration
|
|
"""
|
|
|
|
|
|
# small helper function for modular exponentiation (fast exponentiation algorithm)
|
|
def _modexpt(base: int, exponent: int, modulo_value: int) -> int:
|
|
"""
|
|
Returns the modular exponentiation, that is the value
|
|
of `base ** exponent % modulo_value`, without calculating
|
|
the actual number.
|
|
>>> _modexpt(2, 4, 10)
|
|
6
|
|
>>> _modexpt(2, 1024, 100)
|
|
16
|
|
>>> _modexpt(13, 65535, 7)
|
|
6
|
|
"""
|
|
|
|
if exponent == 1:
|
|
return base
|
|
if exponent % 2 == 0:
|
|
x = _modexpt(base, exponent // 2, modulo_value) % modulo_value
|
|
return (x * x) % modulo_value
|
|
else:
|
|
return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value
|
|
|
|
|
|
def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int:
|
|
"""
|
|
Returns the last 8 digits of the hyperexponentiation of base by
|
|
height, i.e. the number base↑↑height:
|
|
|
|
>>> solution(base=3, height=2)
|
|
27
|
|
>>> solution(base=3, height=3)
|
|
97484987
|
|
>>> solution(base=123, height=456, digits=4)
|
|
2547
|
|
"""
|
|
|
|
# calculate base↑↑height by right-assiciative repeated modular
|
|
# exponentiation
|
|
result = base
|
|
for _ in range(1, height):
|
|
result = _modexpt(base, result, 10**digits)
|
|
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(f"{solution() = }")
|