Project Euler Problem 041

Statement

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.
For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

Solution

Using Python's itertool's permutation object it's pretty easy.

from CommonFunctions import is_prime
from itertools import permutations
 
if __name__ == '__main__':
    result = 0
    for i in range(3,10):
        for x in permutations([str(x) for x in range(1,i+1)]):
            x = int(''.join(x))
            if is_prime(x):
                result = max(result, x)
    print("The result is:", result)

The Python file is available for download here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License