-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Project Euler 62 Solution #3029
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
dhruvmanila
merged 11 commits into
TheAlgorithms:master
from
peteryao7:peteryao7_project-euler-62
Oct 15, 2020
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
edc37bd
Add solution for Project Euler 62
peteryao7 3da81bc
Add doctests and annotate function params and return values for get_d…
peteryao7 4d133e4
Add extra newline between functions to fix flake8 errors
peteryao7 ee984f8
Add extra newlines between function names
peteryao7 a4e322c
Add missing return type for solution()
peteryao7 022fb76
Remove parenthesis from if statement
peteryao7 0f088c6
Remove parentheses from while loop
peteryao7 04a44c5
Add to explanation and fix second Travis build
peteryao7 62aa88c
Compress get_digits(), add tests for solution(), add fstring and posi…
peteryao7 c285709
Remove input param when calling solution()
peteryao7 9616ef6
Remove test case for the answer
peteryao7 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
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
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,64 @@ | ||
""" | ||
Project Euler 62 | ||
https://projecteuler.net/problem=62 | ||
|
||
The cube, 41063625 (345^3), can be permuted to produce two other cubes: | ||
56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube | ||
which has exactly three permutations of its digits which are also cube. | ||
|
||
Find the smallest cube for which exactly five permutations of its digits are | ||
cube. | ||
""" | ||
|
||
from collections import defaultdict | ||
|
||
|
||
def solution(max_base: int = 5) -> int: | ||
""" | ||
Iterate through every possible cube and sort the cube's digits in | ||
ascending order. Sorting maintains an ordering of the digits that allows | ||
you to compare permutations. Store each sorted sequence of digits in a | ||
dictionary, whose key is the sequence of digits and value is a list of | ||
numbers that are the base of the cube. | ||
|
||
Once you find 5 numbers that produce the same sequence of digits, return | ||
the smallest one, which is at index 0 since we insert each base number in | ||
ascending order. | ||
|
||
>>> solution(2) | ||
125 | ||
>>> solution(3) | ||
41063625 | ||
>>> solution(5) | ||
127035954683 | ||
""" | ||
freqs = defaultdict(list) | ||
num = 0 | ||
|
||
while True: | ||
digits = get_digits(num) | ||
freqs[digits].append(num) | ||
|
||
if len(freqs[digits]) == max_base: | ||
base = freqs[digits][0] ** 3 | ||
return base | ||
|
||
num += 1 | ||
|
||
|
||
def get_digits(num: int) -> str: | ||
""" | ||
Computes the sorted sequence of digits of the cube of num. | ||
|
||
>>> get_digits(3) | ||
'27' | ||
>>> get_digits(99) | ||
'027999' | ||
>>> get_digits(123) | ||
'0166788' | ||
""" | ||
return "".join(sorted(list(str(num ** 3)))) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{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.