This time we mix two old topics together and form a new question. This time Project Euler has mixed pandigital numbers and primes and Problem 41 asks us to find the largest such number. The problem description reads

We shall say that ann-digit number is pandigital if it makes use of all the digits 1 tonexactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largestn-digit pandigital prime that exists?

I will start solving it by brute force and as we shall see the approach is possible but very inefficient, so after that I will show you how to speed the process up by using a small property of numbers that I was taught in 4th grade or so – And no, I am not particularly gifted, the trick is just really simple.

## Brute Force

We already have methods for generating prime numbers and checking if a number is pandigital, so all we need is to combine those.

The C# code for the problem looks like

int[] primes = ESieve(987654321); int result = 0; for (int i = primes.Length - 1; i >= 0; i--) { if(isPandigital(primes[i])){ result = primes[i]; break; } }

I generate all primes up to 987654321 which is the largest number that can be pandigital and then loops through them until I find a prime. The only problem is that the execution of the algorithm takes 27 seconds. The result is

The largest Pandigital prime is 7652413 Solution took 27057 ms

That is just not good enough. I know it is within the 1 minute limit proposed by Project Euler, but it isn’t satisfactory for me, so We will have to slightly revise the method.

## Digit sum

The trick I learned in 4th grade was that* a number is divisible by 3 if and only if the digit sum of the number is divisible by 3*. The digit sum is as the name implies the sum of the digits. We can rather easily find the digit sum of pandigital numbers since we know the digits.

1+2+3+4+5+6+7+8+9 = 45

1+2+3+4+5+6+7+8 = 36

1+2+3+4+5+6+7 = 28

1+2+3+4+5+6 = 21

1+2+3+4+5 = 15

1+2+3+4 = 10

1+2+3 = 6

1+2 = 3

From here it is pretty clear that all pandigital numbers except 4 and 7 digit ones are divisible by 3 and thus can’t be primes. I have chose the lazy method and just lowered the upper limit of the prime generation to 7654321 in the previous example which then gives us a running time of 68ms which gives us a factor 400 in speed up.

## Wrapping up

I am pretty sure I could speed up the second part even more if I didn’t use a Sieve method to generate primes, but it is an easy way to do it and the result is pretty fast.

Just for the curious people there is a total of 538 pandigital primes only 4 of those are 4 digit, the rest is 7 digits.

As usual you can find all the code details in the uploaded source code. Did you choose another solution strategy? If so please let me know about it.

I tried it the other way.. first generating the permutations, from the highest possible (987654321), checking each number to see if it’s a prime… it runs in 0.08 seconds (according to ideone) when i start it from k=9 (all 9 digits) but runs in no time at all, when started from 7 (ideone gave as 0 seconds), after considering the fact that 8 and 9 digit pandigital numbers arent primes..

Nice speedup when you use that method. It does make sense that it would be more efficient doing it that way, thanks for the code.

Hi,

knowing, that the permutations 1..7 will be the right ones, I used algorithm from P.E. 24 to generate all permutations of 1..7 and then from the top (7654321) was checking, if the number is a prime. When I found the first, I simply break :).

Kuba’s one is the most efficient I think..

I realised that the number had to be seven digits using pen-and-paper which I won’t describe here. I hence decided to generate primes from 7000000 to 7654321 on the off-chance the solution was above 7000000 (it was my lucky day). This generation is very quick.

I next created a rudimentary IsPandigital function which I am still not happy with – it is messy and looks ugly, but hey – it’s 2am!

I iterate down from 7654321 until the number is both Pandigital

andPrime. This is obviously the answer.My solution runs in 2 milliseconds, by far one of the quickest solutions I’ve made yet.

I popped the source code on pastebin here.

I’m sure further optimisations could be made but I was happy with this result.

Cheers

Julian

Probably a classic example of dumb luck. I do the Euler problems in Python since my motivation was to improve my Python skills. I start with a dumb algorithm if is seems reasonable and improve it.

But instead of generating primes with a sieve, or generating all pandigitals, I just started with 7654321 counting down by 2. I first test if pandigital() and then for primality(). Ran so fast I was sure it was wrong till I checked it.

The solution just happens to be so close to the starting point my algorithm checked less than 1000 candidate number before terminating.

I had expected to write the code to generate pandigital numbers directly in descending order … but it seemed pointless in this case.

hello , i get this number : 7652483

it’s a prime number and is a 7-digit pandigital :

why the solution is 7652413 and not 7652483.

my program looks like:

The reason why 7652413 and not 7652483 is the 8. I didn’t notice it at first but rereading very carefully it says 1 to n, so with a 7 digit number, it’s 1 to 7. The 2nd number has an 8 which is outside of that range and does not have a 1.

I think this is one of those moments when the C++ Standard Library really shines. It makes for very readable, but still efficient code. The following solves the problem in 0.002 seconds.

Looks like it mangled my lessthans and greaterthans. 🙁

I used some ideas of this blog.

The correct solution was calculated in 15 ms.

Even though I used Python, I want to say that I checked a number for being prime by creating a list of its factors and checking the length. It was pretty fast.