Project Euler 111 - Primes with Runs
Official link: https://projecteuler.net/problem=111
Note: Code runs in~350 seconds, too slow!
Thought Process
I have done a bit too many counting problems recently, so although I'm not happy with my solution since it's just brute force I don't care too much.
My idea is simple, given an n-digit number and d our repeating digit, I simply tested all possible numbers. The way I do this is to calculate S(n, d) as follows:
for i in range 0 to n - 1:
There are doing to be n - i, d's in my number, this leaves me with i position left. To generate all possibilities for these i positions I use itertools.product([0,1,2,3,4,5,6,7,8,9], i) to generate all i-tuples of numbers (This essentially is the same as doing a bunch of nested loops (in this case we will do i nested loops))
Now I generate all permutations of [d]*(n - i) + y, where y is an i-tuple from above using itertools.permutations([d]*(n - i) + list(y), n)
Now if our permutation starts with a 0, we exclude it since this is not allowed.
Otherwise, we simply test if it is prime, and if it is I add it to a set to avoid duplicates (yes this is how unbothered I was to optimize)
Interactive Code
Input 2 integers with a space between (n, d)
Code will output S(n, d)