|
| 1 | +""" |
| 2 | +Project Euler Problem 174: https://projecteuler.net/problem=174 |
| 3 | +
|
| 4 | +We shall define a square lamina to be a square outline with a square "hole" so that |
| 5 | +the shape possesses vertical and horizontal symmetry. |
| 6 | +
|
| 7 | +Given eight tiles it is possible to form a lamina in only one way: 3x3 square with a |
| 8 | +1x1 hole in the middle. However, using thirty-two tiles it is possible to form two |
| 9 | +distinct laminae. |
| 10 | +
|
| 11 | +If t represents the number of tiles used, we shall say that t = 8 is type L(1) and |
| 12 | +t = 32 is type L(2). |
| 13 | +
|
| 14 | +Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example, |
| 15 | +N(15) = 832. |
| 16 | +
|
| 17 | +What is ∑ N(n) for 1 ≤ n ≤ 10? |
| 18 | +""" |
| 19 | + |
| 20 | +from collections import defaultdict |
| 21 | +from math import ceil, sqrt |
| 22 | + |
| 23 | + |
| 24 | +def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: |
| 25 | + """ |
| 26 | + Return the sum of N(n) for 1 <= n <= n_limit. |
| 27 | +
|
| 28 | + >>> solution(1000,5) |
| 29 | + 249 |
| 30 | + >>> solution(10000,10) |
| 31 | + 2383 |
| 32 | + """ |
| 33 | + count: defaultdict = defaultdict(int) |
| 34 | + |
| 35 | + for outer_width in range(3, (t_limit // 4) + 2): |
| 36 | + if outer_width * outer_width > t_limit: |
| 37 | + hole_width_lower_bound = max( |
| 38 | + ceil(sqrt(outer_width * outer_width - t_limit)), 1 |
| 39 | + ) |
| 40 | + else: |
| 41 | + hole_width_lower_bound = 1 |
| 42 | + |
| 43 | + hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 |
| 44 | + |
| 45 | + for hole_width in range(hole_width_lower_bound, outer_width - 1, 2): |
| 46 | + count[outer_width * outer_width - hole_width * hole_width] += 1 |
| 47 | + |
| 48 | + return sum(1 for n in count.values() if 1 <= n <= 10) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + print(f"{solution() = }") |
0 commit comments