Python Implementation of Problem 41

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 41

Once I found out where the end was, it seemed to be relatively easy

Problem:

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?

python.src.p0041.main() int
 1"""
 2Project Euler Problem 41
 3
 4Once I found out where the end was, it seemed to be relatively easy
 5
 6Problem:
 7
 8We shall say that an n-digit number is pandigital if it makes use of all the
 9digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is
10also prime.
11
12What is the largest n-digit pandigital prime that exists?
13"""
14
15from .lib.iters import digits
16from .lib.primes import primes
17
18
19def main() -> int:
20    answer = -1
21    for p in primes():
22        cur_digits = tuple(digits(p))
23        num_digits = len(cur_digits)
24        if num_digits > 7:
25            break
26        elif any(
27            digit > num_digits or
28            cur_digits.count(digit) != 1
29            for digit in cur_digits
30        ):
31            continue
32        elif p > answer:
33            answer = p
34    return answer

Tags: pandigital, prime-number, python-iterator