Goofygiraffe's Blog

How I Optimized My Way to the Largest Pandigital Prime: From 362,880 to Just 2,160

When I took on the Project Euler challenge to find the largest pandigital prime, I initially went with the brute force approach. The plan was simple: generate all the pandigital permutations for n, check each one for primality, and move on. It seemed like the natural route, right?

But as soon as I started working with 9-digit pandigital numbers, I quickly realized this wasn’t going to be efficient. With 9!=362,880 permutations to check, it was quickly becoming overwhelming. That’s when I knew I had to come up with something smarter. So, the optimization process began, and by the end, I had reduced the search space by 99.3%, finding the answer in just 0.002 seconds. Here's how I did it.


Step 1: Let’s Think About Math

The first thing I realized was that brute force was never going to get me to the solution quickly enough. I needed a more strategic approach, and that's when number theory stepped in.

One key insight I had was that the sum of the digits of a number determines whether it's divisible by 3. So, if the sum of the digits is divisible by 3, the number can't be prime. Here's how this helped:

That meant I could immediately rule out all 9-digit and 8-digit pandigital numbers. This was a huge win because it reduced the problem from checking over 360,000 possibilities to only 7!=5,040 candidates.


Step 2: Cutting Down Even More

At this point, I was feeling good, but I wasn’t done trimming the fat. I wanted to take a closer look at the numbers I still had. Here’s where I made another key observation:

  1. Remove Numbers Ending in Even Digits or 5
    Any number ending in an even digit or 5 can’t be prime—except for 2 or 5, but those don’t apply here. Because every digit has an equal chance of appearing at the end, this rule let me eliminate:
    • All the numbers ending in even digits (half of them).
    • One-seventh of the remaining numbers (those ending in 5).

After this step, my search space had shrunk to just 2,160 numbers.


Step 3: The Final Touch – Efficient Primality Testing

At this point, I had reduced the search space by over 99%, and with just 2,160 numbers left, I could afford to check each one for primality. But I wasn’t about to go back to the naive approach of checking divisibility by every single number up to n1—that would undo all the hard work I’d done so far!

Instead, I used a more efficient method:

  1. Check divisibility by primes up to n.
  2. Stop as soon as a divisor is found.

It wasn’t the fanciest primality test, but it worked perfectly for the task. And with such a reduced search space, each check was fast enough to make the process feel pretty efficient.


The Answer

After running the optimized algorithm, the largest pandigital prime popped out almost immediately: 7652413 It’s a 7-digit pandigital prime, and it took just 0.002 seconds to find. Considering the problem started with over 360,000 possibilities, I’d say that’s a pretty solid result!


Why Optimization Is So Rewarding

This problem was a nice reminder of why I love tackling challenges like this. It’s not just about getting to the answer—it’s about the process. Finding ways to reduce unnecessary work and think creatively makes solving the problem so much more satisfying. Plus, there’s something deeply rewarding about slashing the search space from 362,880 to just 2,160, knowing that every step I took was backed by some solid reasoning.

Optimization isn’t just about saving time; it’s about seeing the problem in a new light. It’s like peeling away layers until you find the elegant core beneath. I hope reading about this process helps inspire you to think more critically and creatively the next time you face a tough problem.

Thanks for reading—and happy problem-solving!

#Optimizations #Problem Solving #Project Euler #math