-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added solution for Project Euler problem 38. #3115
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef0d092
Added solution for Project Euler problem 38. Fixes: #2695
fpringle b50f045
Update docstring and 0-padding in directory name. Reference: #3256
fpringle bea153c
Renamed is_9_palindromic to is_9_pandigital.
fpringle 44c3dec
Changed just-in-case return value for solution() to None.
fpringle 9671f23
Moved exmplanation to module-level docstring and deleted unnecessary …
fpringle 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,77 @@ | ||
""" | ||
Project Euler Problem 38: https://projecteuler.net/problem=38 | ||
Take the number 192 and multiply it by each of 1, 2, and 3: | ||
192 × 1 = 192 | ||
192 × 2 = 384 | ||
192 × 3 = 576 | ||
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call | ||
192384576 the concatenated product of 192 and (1,2,3) | ||
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, | ||
giving the pandigital, 918273645, which is the concatenated product of 9 and | ||
(1,2,3,4,5). | ||
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the | ||
concatenated product of an integer with (1,2, ... , n) where n > 1? | ||
Solution: | ||
Since n>1, the largest candidate for the solution will be a concactenation of | ||
a 4-digit number and its double, a 5-digit number. | ||
Let a be the 4-digit number. | ||
a has 4 digits => 1000 <= a < 10000 | ||
2a has 5 digits => 10000 <= 2a < 100000 | ||
=> 5000 <= a < 10000 | ||
The concatenation of a with 2a = a * 10^5 + 2a | ||
so our candidate for a given a is 100002 * a. | ||
We iterate through the search space 5000 <= a < 10000 in reverse order, | ||
calculating the candidates for each a and checking if they are 1-9 pandigital. | ||
In case there are no 4-digit numbers that satisfy this property, we check | ||
the 3-digit numbers with a similar formula (the example a=192 gives a lower | ||
bound on the length of a): | ||
a has 3 digits, etc... | ||
=> 100 <= a < 334, candidate = a * 10^6 + 2a * 10^3 + 3a | ||
= 1002003 * a | ||
""" | ||
|
||
from typing import Union | ||
|
||
|
||
def is_9_pandigital(n: int) -> bool: | ||
""" | ||
Checks whether n is a 9-digit 1 to 9 pandigital number. | ||
>>> is_9_pandigital(12345) | ||
False | ||
>>> is_9_pandigital(156284973) | ||
True | ||
>>> is_9_pandigital(1562849733) | ||
False | ||
""" | ||
s = str(n) | ||
return len(s) == 9 and set(s) == set("123456789") | ||
|
||
|
||
def solution() -> Union[int, None]: | ||
""" | ||
Return the largest 1 to 9 pandigital 9-digital number that can be formed as the | ||
concatenated product of an integer with (1,2,...,n) where n > 1. | ||
""" | ||
for base_num in range(9999, 4999, -1): | ||
candidate = 100002 * base_num | ||
if is_9_pandigital(candidate): | ||
return candidate | ||
|
||
for base_num in range(333, 99, -1): | ||
candidate = 1002003 * base_num | ||
if is_9_pandigital(candidate): | ||
return candidate | ||
|
||
return None | ||
|
||
|
||
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.