Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 730 Bytes

41. Pandigital prime.md

File metadata and controls

25 lines (18 loc) · 730 Bytes

Question

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

It is easy to see that all 8-digit and 9-digit pandigital numbers are divisible by 3.

from itertools import permutations

def p41():
    for p in permutations('7654321'):
        if p[-1] in {'2','4','5','6','8'}: continue
        if isprime(int(''.join(p))): break  # isprime function see link below
    return ''.join(p)

Answer

7652413

Note