-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Project Euler 206 Solution #3042
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
Closed
peteryao7
wants to merge
8
commits into
TheAlgorithms:master
from
peteryao7:peteryao7_project-euler-206
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
259c07b
Add solution for Project Euler 206, Fixes: #2695
peteryao7 eda8733
Add fstring for solution()
peteryao7 72da9bc
Merge branch 'master' into peteryao7_project-euler-206
peteryao7 acaac2d
Readd deleted problems in directory
peteryao7 ca306ea
Move up explanation to module code block
peteryao7 36fcaa7
Clear up module explanation of the solution, move solution() below he…
peteryao7 130a820
Merge branch 'master' into peteryao7_project-euler-206
peteryao7 e62ae73
updating DIRECTORY.md
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,65 @@ | ||
""" | ||
Project Euler 206 | ||
https://projecteuler.net/problem=206 | ||
|
||
Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, | ||
where each “_” is a single digit. | ||
|
||
----- | ||
|
||
Instead of computing every single permutation of that number and going | ||
through a 10^9 search space, we can narrow it down considerably. | ||
|
||
If the square ends in a 0, then the square root must also end in a 0. Thus, | ||
the last missing digit must be 0 and the square root is a multiple of 10. | ||
We can narrow the search space down to the first 8 digits and multiply the | ||
result of that by 10 at the end. | ||
|
||
Now the last digit is a 9, which can only happen if the square root ends | ||
in a 3 or 7. We can either start checking for the square root from | ||
101010103, which is the closest square root of 10203040506070809 that ends | ||
in 3 or 7, or 138902663, the closest square root of 1929394959697989. The | ||
problem says there's only 1 answer, so starting at either point is fine, | ||
but the result happens to be much closer to the latter. | ||
""" | ||
|
||
|
||
def solution() -> int: | ||
""" | ||
Returns the first integer whose square is of the form 1_2_3_4_5_6_7_8_9_0. | ||
""" | ||
num = 138902663 | ||
|
||
while not is_square_form(num * num): | ||
if num % 10 == 3: | ||
num -= 6 # (3 - 6) % 10 = 7 | ||
else: | ||
num -= 4 # (7 - 4) % 10 = 3 | ||
|
||
return num * 10 | ||
|
||
|
||
def is_square_form(num: int) -> bool: | ||
""" | ||
Determines if num is in the form 1_2_3_4_5_6_7_8_9 | ||
|
||
>>> is_square_form(1) | ||
False | ||
>>> is_square_form(112233445566778899) | ||
True | ||
>>> is_square_form(123456789012345678) | ||
False | ||
""" | ||
digit = 9 | ||
|
||
while num > 0: | ||
if num % 10 != digit: | ||
return False | ||
num //= 100 | ||
digit -= 1 | ||
|
||
return True | ||
|
||
|
||
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.