|
| 1 | +""" |
| 2 | +It turns out that 12 cm is the smallest length of wire that can be bent to form an |
| 3 | +integer sided right angle triangle in exactly one way, but there are many more examples. |
| 4 | +
|
| 5 | +12 cm: (3,4,5) |
| 6 | +24 cm: (6,8,10) |
| 7 | +30 cm: (5,12,13) |
| 8 | +36 cm: (9,12,15) |
| 9 | +40 cm: (8,15,17) |
| 10 | +48 cm: (12,16,20) |
| 11 | +
|
| 12 | +In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided |
| 13 | +right angle triangle, and other lengths allow more than one solution to be found; for |
| 14 | +example, using 120 cm it is possible to form exactly three different integer sided |
| 15 | +right angle triangles. |
| 16 | +
|
| 17 | +120 cm: (30,40,50), (20,48,52), (24,45,51) |
| 18 | +
|
| 19 | +Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can |
| 20 | +exactly one integer sided right angle triangle be formed? |
| 21 | +""" |
| 22 | + |
| 23 | +from collections import defaultdict |
| 24 | +from math import gcd |
| 25 | +from typing import DefaultDict |
| 26 | + |
| 27 | + |
| 28 | +def solution(limit: int = 1500000) -> int: |
| 29 | + """ |
| 30 | + Return the number of values of L <= limit such that a wire of length L can be |
| 31 | + formmed into an integer sided right angle triangle in exactly one way. |
| 32 | + """ |
| 33 | + freqs: DefaultDict = defaultdict(int) |
| 34 | + m = 2 |
| 35 | + while 2 * m * (m + 1) <= limit: |
| 36 | + for n in range((m % 2) + 1, m, 2): |
| 37 | + if gcd(m, n) > 1: |
| 38 | + continue |
| 39 | + perim = 2 * m * (m + n) |
| 40 | + for p in range(perim, limit + 1, perim): |
| 41 | + freqs[p] += 1 |
| 42 | + m += 1 |
| 43 | + return sum(1 for v in freqs.values() if v == 1) |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + print(solution()) |
0 commit comments