Project Euler 118 - Pandigital Prime Sets
Official link: https://projecteuler.net/problem=118
Thought Process
I thought of an idea quickly but it was quite a fun challenge to implement it!
Firstly, using itertools.permutations("123456789", i) I generate all i-digit numbers containing at most 1 of each number, and then test if those numbers are prime and I group them in an array P, that is P[j] contains all primes with j-digits containing at most one of each number. This is much faster than generating all primes up till 10^9 and then finding the ones containing at most one of each digit.
Next my idea was that given a partition of 9 say for example (1, 1, 2, 2, 3) I would test all combinations of primes from P[1], P[1], P[2], P[2], P[3] and see which ones form pandigital sets.
My first try I basically did a recursive backtracking type of solution where I would test everything from P[3], then move back to where I was in P[2] and move forward 1, etc, etc (Similar to Problem 96), it was a fun problem and definietly satisfying when I got the answer but actually while writing this post, I realized I could make my life so much easier and use itertools.product(*[P[1], P[1], P[2], P[2], P[3]]) (For new python users a * infront of a list unpacks the list).
At least I felt a bit better since the solution I spent hours coding was twice as fast the new one I thought of!! (Although it took 5 mins to write...)
I add both codes below, the coed for is_prime and partition functions are linked above (They are part of my library of functions)
Interactive Code
This problem is not suitable for interactive code