Returns the maximum pandigital prime number of length n. If there are none, then it will return 0. >>> solution(2) 0 >>> solution(4) 4231 >>> solution(7) 7652413
(n: int = 7)
| 57 | |
| 58 | |
| 59 | def solution(n: int = 7) -> int: |
| 60 | """ |
| 61 | Returns the maximum pandigital prime number of length n. |
| 62 | If there are none, then it will return 0. |
| 63 | >>> solution(2) |
| 64 | 0 |
| 65 | >>> solution(4) |
| 66 | 4231 |
| 67 | >>> solution(7) |
| 68 | 7652413 |
| 69 | """ |
| 70 | pandigital_str = "".join(str(i) for i in range(1, n + 1)) |
| 71 | perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)] |
| 72 | pandigitals = [num for num in perm_list if is_prime(num)] |
| 73 | return max(pandigitals) if pandigitals else 0 |
| 74 | |
| 75 | |
| 76 | if __name__ == "__main__": |