# Project Euler #51: Prime digit replacements

# Project Euler #51: Prime digit replacements

+ 0 comments Took me some time to realise the output of

`5 1 2`

is`10007 10009`

instead of`10007 10037`

(Python, it gave wrong answer for Test #3 and #10).Was trying to save time by returning the first plausible result from digit replacement generated by looping through digits from

`0`

to`9`

, and it turns out that way it's not necessarily the minimum result.Eventually I computed all plausible results from all digit replacement and its corresponding permutations. I would say it's too ugly to my taste.

**EDIT**: Made use of generator combining permutations with`deque()`

to`yield`

the next smallest replacement position, so it does not have to generate the whole thing in advance. Sounds simple, troublesome to implement. The slowest time for me is 4.74s for #18. Previous version took almost 8s...

+ 0 comments test cases are weak. for input 3 2 1 my solution is giving output 113, but it should be 101.

+ 2 comments Hey, I seem to be getting WA for cases 7 and 8 only. Any boundary cases that I need to be careful about?

+ 0 comments (a) Prime sieve up to 10^7

(b) Look for primes nearest to 10^(N - 1) and go up

(c) Assemble masks by recursion, ensuring along the way that all replaced digits are equal

+ 2 comments Am I missing something, or for sample input #01 (2 1 3) there are many possible answers? ((11, 13, 17), (11, 13, 19), (11, 31, 41), (11, 31, 61) and so on)

If so, how should we decide which solution to choose?

Sort 17 Discussions, By:

Please Login in order to post a comment