Returns a dictionary with keys as the perimeter of a right angled triangle and value as the number of corresponding triplets. >>> pythagorean_triple(15) Counter({12: 1}) >>> pythagorean_triple(40) Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1}) >>> pythagorean_triple
(max_perimeter: int)
| 15 | |
| 16 | |
| 17 | def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]: |
| 18 | """ |
| 19 | Returns a dictionary with keys as the perimeter of a right angled triangle |
| 20 | and value as the number of corresponding triplets. |
| 21 | >>> pythagorean_triple(15) |
| 22 | Counter({12: 1}) |
| 23 | >>> pythagorean_triple(40) |
| 24 | Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1}) |
| 25 | >>> pythagorean_triple(50) |
| 26 | Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1}) |
| 27 | """ |
| 28 | triplets: typing.Counter[int] = Counter() |
| 29 | for base in range(1, max_perimeter + 1): |
| 30 | for perpendicular in range(base, max_perimeter + 1): |
| 31 | hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5 |
| 32 | if hypotenuse == int(hypotenuse): |
| 33 | perimeter = int(base + perpendicular + hypotenuse) |
| 34 | if perimeter > max_perimeter: |
| 35 | continue |
| 36 | triplets[perimeter] += 1 |
| 37 | return triplets |
| 38 | |
| 39 | |
| 40 | def solution(n: int = 1000) -> int: |