pandigital prime problem 41 https projecteuler netproblem41 we shall say that an ndigit number is pandigital if it makes use of all the digits 1 to n exactly once for example 2143 is a 4digit pandigital and is also prime what is the largest ndigit pandigital prime that exists all pandigital numbers except for 1 4 7 pandigital numbers are divisible by 3 so we will check only 7 digit pandigital numbers to obtain the largest possible pandigital prime checks to see if a number is a prime in osqrtn a number is prime if it has exactly two factors 1 and itself isprime0 false isprime1 false isprime2 true isprime3 true isprime27 false isprime87 false isprime563 true isprime2999 true isprime67483 false 2 and 3 are primes negatives 0 1 all even numbers all multiples of 3 are not primes all primes number are in format of 6k 1 returns the maximum pandigital prime number of length n if there are none then it will return 0 solution2 0 solution4 4231 solution7 7652413 checks to see if a number is a prime in o sqrt n a number is prime if it has exactly two factors 1 and itself is_prime 0 false is_prime 1 false is_prime 2 true is_prime 3 true is_prime 27 false is_prime 87 false is_prime 563 true is_prime 2999 true is_prime 67483 false 2 and 3 are primes negatives 0 1 all even numbers all multiples of 3 are not primes all primes number are in format of 6k 1 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
from __future__ import annotations

import math
from itertools import permutations


def is_prime(number: int) -> bool:
    

    if 1 < number < 4:
        
        return True
    elif number < 2 or number % 2 == 0 or number % 3 == 0:
        
        return False

    
    for i in range(5, int(math.sqrt(number) + 1), 6):
        if number % i == 0 or number % (i + 2) == 0:
            return False
    return True


def solution(n: int = 7) -> int:
    
    pandigital_str = "".join(str(i) for i in range(1, n + 1))
    perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)]
    pandigitals = [num for num in perm_list if is_prime(num)]
    return max(pandigitals) if pandigitals else 0


if __name__ == "__main__":
    print(f"{solution() = }")