From 6c7b67325758c5d4f8d4919c2ab80b7011dcc157 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Fri, 9 Oct 2020 00:16:56 +0200 Subject: [PATCH 1/3] Added solution for Project Euler problemm problem 173. Fixes: #2695 --- project_euler/problem_173/__init__.py | 0 project_euler/problem_173/sol1.py | 37 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 project_euler/problem_173/__init__.py create mode 100644 project_euler/problem_173/sol1.py diff --git a/project_euler/problem_173/__init__.py b/project_euler/problem_173/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_173/sol1.py b/project_euler/problem_173/sol1.py new file mode 100644 index 000000000000..ebb0e7326476 --- /dev/null +++ b/project_euler/problem_173/sol1.py @@ -0,0 +1,37 @@ +""" +We shall define a square lamina to be a square outline with a square "hole" so that +the shape possesses vertical and horizontal symmetry. For example, using exactly +thirty-two square tiles we can form two different square laminae: + +With one-hundred tiles, and not necessarily using all of the tiles at one time, it is +possible to form forty-one different square laminae. + +Using up to one million tiles how many different square laminae can be formed? +""" + + +from math import ceil, sqrt + + +def solution(): + """ + + """ + LIMIT = 10 ** 6 + answer = 0 + + for outer_width in range(3, (LIMIT // 4) + 2): + if outer_width ** 2 > LIMIT: + hole_width_lower_bound = max(ceil(sqrt(outer_width ** 2 - LIMIT)), 1) + else: + hole_width_lower_bound = 1 + if (outer_width - hole_width_lower_bound) % 2: + hole_width_lower_bound += 1 + + answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 + + return answer + + +if __name__ == "__main__": + print(solution()) From b8a96476365147de181b03c42ffb8ff923362e46 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Fri, 9 Oct 2020 00:27:34 +0200 Subject: [PATCH 2/3] Added docstring --- project_euler/problem_173/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_173/sol1.py b/project_euler/problem_173/sol1.py index ebb0e7326476..d5c47521a566 100644 --- a/project_euler/problem_173/sol1.py +++ b/project_euler/problem_173/sol1.py @@ -15,7 +15,8 @@ def solution(): """ - + Return the number of different square laminae that can be formed using up to + one million tiles. """ LIMIT = 10 ** 6 answer = 0 From 44667d4ace80ecfda91d425f12a813e20ba9a44d Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Thu, 15 Oct 2020 10:06:15 +0200 Subject: [PATCH 3/3] Update formatting, doctest and annotations. Reference: #3256 --- project_euler/problem_173/sol1.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_173/sol1.py b/project_euler/problem_173/sol1.py index d5c47521a566..d539b1437ef1 100644 --- a/project_euler/problem_173/sol1.py +++ b/project_euler/problem_173/sol1.py @@ -1,4 +1,6 @@ """ +Project Euler Problem 173: https://projecteuler.net/problem=173 + We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and horizontal symmetry. For example, using exactly thirty-two square tiles we can form two different square laminae: @@ -13,17 +15,18 @@ from math import ceil, sqrt -def solution(): +def solution(limit: int = 1000000) -> int: """ Return the number of different square laminae that can be formed using up to one million tiles. + >>> solution(100) + 41 """ - LIMIT = 10 ** 6 answer = 0 - for outer_width in range(3, (LIMIT // 4) + 2): - if outer_width ** 2 > LIMIT: - hole_width_lower_bound = max(ceil(sqrt(outer_width ** 2 - LIMIT)), 1) + for outer_width in range(3, (limit // 4) + 2): + if outer_width ** 2 > limit: + hole_width_lower_bound = max(ceil(sqrt(outer_width ** 2 - limit)), 1) else: hole_width_lower_bound = 1 if (outer_width - hole_width_lower_bound) % 2: @@ -35,4 +38,4 @@ def solution(): if __name__ == "__main__": - print(solution()) + print(f"{solution() = }")