From 84459dc706feaf959ddcfd96f69e166349676bbf Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Fri, 9 Oct 2020 01:11:48 +0200 Subject: [PATCH 1/5] Added solution for Project Euler problem 174. Fixes: #2695 --- project_euler/problem_174/__init__.py | 0 project_euler/problem_174/sol1.py | 47 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 project_euler/problem_174/__init__.py create mode 100755 project_euler/problem_174/sol1.py diff --git a/project_euler/problem_174/__init__.py b/project_euler/problem_174/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py new file mode 100755 index 000000000000..53ea8f6893f2 --- /dev/null +++ b/project_euler/problem_174/sol1.py @@ -0,0 +1,47 @@ +""" +We shall define a square lamina to be a square outline with a square "hole" so that +the shape possesses vertical and horizontal symmetry. + +Given eight tiles it is possible to form a lamina in only one way: 3x3 square with a +1x1 hole in the middle. However, using thirty-two tiles it is possible to form two +distinct laminae. + +If t represents the number of tiles used, we shall say that t = 8 is type L(1) and +t = 32 is type L(2). + +Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example, +N(15) = 832. + +What is ∑ N(n) for 1 ≤ n ≤ 10? +""" + + +from math import ceil, sqrt +from collections import defaultdict + + +def solution(): + """ + Return the sum of N(n) for 1 <= n <= 10. + """ + LIMIT = 10 ** 6 + count = defaultdict(int) + + for outer_width in range(3, (LIMIT // 4) + 2): + if outer_width * outer_width > LIMIT: + hole_width_lower_bound = max( + ceil(sqrt(outer_width * outer_width - LIMIT)), 1 + ) + else: + hole_width_lower_bound = 1 + + hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 + + for hole_width in range(hole_width_lower_bound, outer_width - 1, 2): + count[outer_width * outer_width - hole_width * hole_width] += 1 + + return sum(1 for n in count.values() if 1 <= n <= 10) + + +if __name__ == "__main__": + print(solution()) From 7d37713a673b17e838342f867f058f2b7ec542ab Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Fri, 9 Oct 2020 01:40:55 +0200 Subject: [PATCH 2/5] Fixed import order and removed executable permission from sol1.py --- project_euler/problem_174/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 project_euler/problem_174/sol1.py diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py old mode 100755 new mode 100644 index 53ea8f6893f2..7f2efff00516 --- a/project_euler/problem_174/sol1.py +++ b/project_euler/problem_174/sol1.py @@ -16,8 +16,8 @@ """ -from math import ceil, sqrt from collections import defaultdict +from math import ceil, sqrt def solution(): From ebf25d3a4e6f44528c209b34a005225d9964a1a1 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Thu, 15 Oct 2020 10:13:01 +0200 Subject: [PATCH 3/5] Update docstrings, doctests and annotations. Reference: #3256 --- project_euler/problem_174/sol1.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py index 7f2efff00516..d391fd231a17 100644 --- a/project_euler/problem_174/sol1.py +++ b/project_euler/problem_174/sol1.py @@ -1,4 +1,6 @@ """ +Project Euler Problem 174: https://projecteuler.net/problem=174 + We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and horizontal symmetry. @@ -15,22 +17,24 @@ What is ∑ N(n) for 1 ≤ n ≤ 10? """ - from collections import defaultdict from math import ceil, sqrt -def solution(): +def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: """ Return the sum of N(n) for 1 <= n <= 10. + >>> solution(1000,5) + 249 + >>> solution(10000,10) + 2383 """ - LIMIT = 10 ** 6 - count = defaultdict(int) + count: defaultdict = defaultdict(int) - for outer_width in range(3, (LIMIT // 4) + 2): - if outer_width * outer_width > LIMIT: + for outer_width in range(3, (t_limit // 4) + 2): + if outer_width * outer_width > t_limit: hole_width_lower_bound = max( - ceil(sqrt(outer_width * outer_width - LIMIT)), 1 + ceil(sqrt(outer_width * outer_width - t_limit)), 1 ) else: hole_width_lower_bound = 1 @@ -44,4 +48,4 @@ def solution(): if __name__ == "__main__": - print(solution()) + print(f"{solution() = }") From 698ea1e64fda299294fdd607c9b679d7cfcdd6fb Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Thu, 15 Oct 2020 10:55:57 +0200 Subject: [PATCH 4/5] Update docstring --- project_euler/problem_174/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py index d391fd231a17..cf739e7858f6 100644 --- a/project_euler/problem_174/sol1.py +++ b/project_euler/problem_174/sol1.py @@ -23,7 +23,7 @@ def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: """ - Return the sum of N(n) for 1 <= n <= 10. + Return the sum of N(n) for 1 <= n <= n_limit. >>> solution(1000,5) 249 >>> solution(10000,10) From 37ae5c7400a30ccefea709f0157dee91a792b195 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Fri, 16 Oct 2020 15:02:56 +0530 Subject: [PATCH 5/5] Update sol1.py --- project_euler/problem_174/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py index cf739e7858f6..cbc0df5a9d65 100644 --- a/project_euler/problem_174/sol1.py +++ b/project_euler/problem_174/sol1.py @@ -24,6 +24,7 @@ def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: """ Return the sum of N(n) for 1 <= n <= n_limit. + >>> solution(1000,5) 249 >>> solution(10000,10)