An n-digit number is pandigital if it contains all digits from 1 to n exactly once.

I can’t be bothered to write my own permutation function, the RcppAlgos package can do it. I’ll also need tidyverse and numbers:

library(RcppAlgos)
## Warning: package 'RcppAlgos' was built under R version 4.5.1
library(tidyverse)
library(numbers)

First, generate all pandigital numbers for n = 4 to 9.

possible <- list()
for(i in 4:9){
possible[[i]] <- permuteGeneral(1:i) |> 
    apply(1, paste0, collapse = "")}

Unlist and convert to numeric:

possible <- possible |> 
    unlist() |> 
    as.numeric()

Remove all even numbers. And numbers divisible by 5:

possible <- possible[possible%%2 != 0]
possible <- possible[possible%%5 != 0]

And then subset only the primes - and find the largest:

answer <- possible[isPrime(possible)] |> max()