Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. >>> solution(20000) 19591 >>> solution(30000) 29992 >>> solution(40000) 39893
(n: int = 998001)
| 14 | |
| 15 | |
| 16 | def solution(n: int = 998001) -> int: |
| 17 | """ |
| 18 | Returns the largest palindrome made from the product of two 3-digit |
| 19 | numbers which is less than n. |
| 20 | |
| 21 | >>> solution(20000) |
| 22 | 19591 |
| 23 | >>> solution(30000) |
| 24 | 29992 |
| 25 | >>> solution(40000) |
| 26 | 39893 |
| 27 | """ |
| 28 | |
| 29 | answer = 0 |
| 30 | for i in range(999, 99, -1): # 3 digit numbers range from 999 down to 100 |
| 31 | for j in range(999, 99, -1): |
| 32 | product_string = str(i * j) |
| 33 | if product_string == product_string[::-1] and i * j < n: |
| 34 | answer = max(answer, i * j) |
| 35 | return answer |
| 36 | |
| 37 | |
| 38 | if __name__ == "__main__": |