From 259c07b479c84c5ef51e8fdbc0ad3aa8ce3d2081 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Thu, 8 Oct 2020 01:44:16 -0700 Subject: [PATCH 1/6] Add solution for Project Euler 206, Fixes: #2695 --- DIRECTORY.md | 2 + project_euler/problem_206/__init__.py | 0 project_euler/problem_206/sol1.py | 61 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 project_euler/problem_206/__init__.py create mode 100644 project_euler/problem_206/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 6a3d31709ed6..2fe6d14f2754 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -575,6 +575,8 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol2.py) * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol3.py) * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol4.py) + * Problem 206 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 21 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_21/sol1.py) * Problem 22 diff --git a/project_euler/problem_206/__init__.py b/project_euler/problem_206/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py new file mode 100644 index 000000000000..c572312e7b2f --- /dev/null +++ b/project_euler/problem_206/sol1.py @@ -0,0 +1,61 @@ +""" +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. +""" + + +def solution() -> int: + """ + 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. + """ + 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(solution()) From eda873342ace54e5ad93917c347f09b628087194 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 14 Oct 2020 21:09:57 -0700 Subject: [PATCH 2/6] Add fstring for solution() --- project_euler/problem_206/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py index c572312e7b2f..08c152262420 100644 --- a/project_euler/problem_206/sol1.py +++ b/project_euler/problem_206/sol1.py @@ -58,4 +58,4 @@ def is_square_form(num: int) -> bool: if __name__ == "__main__": - print(solution()) + print(f"{solution() = }") From acaac2d73c7c6b0e158395468e35718b01cac3f8 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Thu, 15 Oct 2020 20:29:58 -0700 Subject: [PATCH 3/6] Readd deleted problems in directory --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c15e3ae50d59..25e8a34c6ec2 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -685,6 +685,12 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py) * Problem 120 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py) + * Problem 125 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py) + * Problem 173 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py) + * Problem 191 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) * Problem 206 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 234 From ca306ea301968872ed5ddd1152416f8d35e1d868 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Thu, 15 Oct 2020 20:35:59 -0700 Subject: [PATCH 4/6] Move up explanation to module code block --- project_euler/problem_206/sol1.py | 32 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py index 08c152262420..e493488d2ad3 100644 --- a/project_euler/problem_206/sol1.py +++ b/project_euler/problem_206/sol1.py @@ -4,25 +4,29 @@ 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: """ - 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. + Returns the first integer whose square is of the form 1_2_3_4_5_6_7_8_9_0. """ num = 138902663 From 36fcaa7f1c021a8d0d2eb99463e236a893bd0bf3 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Fri, 23 Oct 2020 19:49:08 -0700 Subject: [PATCH 5/6] Clear up module explanation of the solution, move solution() below helper method --- project_euler/problem_206/sol1.py | 49 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py index e493488d2ad3..ffac2b32aa77 100644 --- a/project_euler/problem_206/sol1.py +++ b/project_euler/problem_206/sol1.py @@ -1,6 +1,5 @@ """ -Project Euler 206 -https://projecteuler.net/problem=206 +Project Euler Problem 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. @@ -16,27 +15,22 @@ 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. -""" +in a 3 or 7. From this point, we can try one of two different methods to find +the answer: +1. Start at the lowest possible base number whose square would be in the +format, and count up. The base we would start at is 101010103, whose square is +the closest number to 10203040506070809. Alternate counting up by 4 and 6 so +the last digit of the base is always a 3 or 7. -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 +2. Start at the highest possible base number whose square would be in the +format, and count down. That base would be 138902663, whose square is the +closest number to 1929394959697989. Alternate counting down by 6 and 4 so the +last digit of the base is always a 3 or 7. - return num * 10 +The solution does option 2 because the answer happens to be much closer to the +starting point. +""" def is_square_form(num: int) -> bool: @@ -61,5 +55,20 @@ def is_square_form(num: int) -> bool: return True +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 + + if __name__ == "__main__": print(f"{solution() = }") From e62ae734a5046bb60a8ee00d89a145afc6256ee7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 30 Oct 2020 01:21:32 +0000 Subject: [PATCH 6/6] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6ad9638150e4..0cecae28d51d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -674,6 +674,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_056/sol1.py) * Problem 057 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_057/sol1.py) + * Problem 058 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_058/sol1.py) * Problem 062 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) * Problem 063 @@ -699,6 +701,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py) * Problem 081 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_081/sol1.py) + * Problem 087 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_087/sol1.py) * Problem 091 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py) * Problem 097 @@ -819,6 +823,7 @@ * [Prefix Function](https://github.com/TheAlgorithms/Python/blob/master/strings/prefix_function.py) * [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py) * [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py) + * [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py) * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) * [Swap Case](https://github.com/TheAlgorithms/Python/blob/master/strings/swap_case.py)