Precomputing squares and checking if a^2 + b^2 is the square by set look-up. >>> solution(12) 60 >>> solution(36) 1620
(n: int = 1000)
| 34 | |
| 35 | |
| 36 | def solution(n: int = 1000) -> int: |
| 37 | """ |
| 38 | Precomputing squares and checking if a^2 + b^2 is the square by set look-up. |
| 39 | |
| 40 | >>> solution(12) |
| 41 | 60 |
| 42 | >>> solution(36) |
| 43 | 1620 |
| 44 | """ |
| 45 | |
| 46 | squares = get_squares(n) |
| 47 | squares_set = set(squares) |
| 48 | for a in range(1, n // 3): |
| 49 | for b in range(a + 1, (n - a) // 2 + 1): |
| 50 | if ( |
| 51 | squares[a] + squares[b] in squares_set |
| 52 | and squares[n - a - b] == squares[a] + squares[b] |
| 53 | ): |
| 54 | return a * b * (n - a - b) |
| 55 | |
| 56 | return -1 |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |