MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_041/sol1.py:59–73  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

57
58
59def 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
76if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

is_primeFunction · 0.70

Tested by

no test coverage detected