def product_pandigital(a,b):
    c = a * b
    digits = [int(x) for x in list(str(a)) + list(str(b)) + list(str(c))]
    digits = int(''.join(map(str, digits)))
    if is_pandigital(digits, 9):
        return True
    return False
def solve():
    guess = 50000  # definite maximum
    while True:
        check_this = int(str(guess) + str(2 * guess))  # base case
        n = 3
        while check_this <= 987654321:
            if is_pandigital(check_this, 9):
                return check_this
            check_this = int(str(check_this) + str(n * guess))
            n += 1
        guess -= 1
def solve():
    best_case = '987654321' # 9 digit pandigital would be ideal
    while best_case != '':
        guess_perms = sorted([int(''.join(p)) for p in permutations(best_case)], reverse = True) # find all pandigitals

        for guess in guess_perms:
            if is_pandigital(guess, len(best_case)) and is_prime(guess):
                return guess
            else:
                guess -= 1
            
        best_case = best_case[1:] # truncate if we give up on this many digits