-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add solution for Project Euler problem 188 #2880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc4a979
Project Euler problem 188 solution
darkstar 8e6dab9
fix superscript notation
darkstar 2fa521b
split out modexpt() function, and rename parameters
darkstar dcc9aa4
Add some more doctest, and add type hints
darkstar bee3abf
Add some reference links
darkstar c8747e4
Update docstrings and mark helper function private
darkstar 780ebe0
Fix doctests and remove/improve redundant comments
darkstar 2de8c59
fix as per style guide
darkstar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
The hyperexponentiation of a number | ||
Problem 188 | ||
|
||
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. | ||
""" | ||
|
||
|
||
def solution(a=1777, k=1855, digits=8): | ||
darkstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Returns the last 8 digits of the hyperexponentiation of a by k. | ||
|
||
>>> solution() | ||
95962097 | ||
darkstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> solution(3, 2) | ||
27 | ||
>>> solution(3, 3) | ||
97484987 | ||
""" | ||
|
||
# we calculate everything modulo 10^8, since we only care about the | ||
# last 8 digits | ||
modulo_value = 10 ** digits | ||
|
||
# small helper function for modular exponentiation, to keep the result | ||
# values small enough | ||
def modexpt(base, exponent): | ||
darkstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if exponent == 1: | ||
return base | ||
if exponent % 2 == 0: | ||
x = modexpt(base, exponent / 2) % modulo_value | ||
return (x * x) % modulo_value | ||
else: | ||
return (base * modexpt(base, exponent - 1)) % modulo_value | ||
|
||
# calculate a ↑↑ k (mod modulo_value) | ||
result = a | ||
for i in range(1, k): | ||
result = modexpt(a, result) % modulo_value | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.