[Math] Bruteforcing a keypad lock

combinatoricspermutationspuzzle

A particular lock at my university has a keypad with the digits 1-5 on it. When pressed in the correct permutation of those five digits, the lock will open.

Obviously, since there are only 120 permutations, we can bruteforce the lock in 600 presses. But we can do better! The sequence 1234512 actually tests three distinct sequences with only seven presses – 12345, 23451, and 34512.

What's the fastest strategy to bruteforce this lock?

(and for bonus points, other locks with more numbers, longer passcodes, etc.)

(I've taken a few stabs at this problem with no progress to speak of – particularly, De Bruijn sequences are not directly relevant here)

Best Answer

We want to find a word over the alphabet $\{1,\ldots,n\}$ that is as short as possible and contains all $n!$ permutations of the alphabet as infixes. Let $\ell_n$ denote the shortest achievable length. Clearly, $$\tag1\ell_n\ge n!+(n-1).$$ Of course, $(1)$ is somewhat optimistic because it requires $(n-1)$-overlaps between all consecutive permutations, but that is only possible for cyclicly equivalent permutations. As there are $(n-1)!$ equivalence classes under cyclic equivalence, and switching between these requires us to "waste" a symbol, we find that $$\tag2\ell_n\ge n!+(n-1)+(n-1)!-1=n!+(n-1)!+(n-2).$$ In particular, inequality $(2)$ is sharp for the first few cases $\ell_1=1$, $\ell_2=3$ (from "121"), $\ell_3=9$ (from "312313213"). However, it seems that that $\ell_4=33$ (from "314231432143124313421341234132413").

Feeding these few values into the OEIS search engine reveals http://oeis.org/A180632 and we see that the exact values seem to be known only up to $\ell_5=153$ (which is your original problem)! Quoting the known bounds from there, we have $$ \ell_n\ge n! + (n-1)! + (n-2)! + n-3$$ (which can supposedly be shown similarly to how we found ($2)$ above) and $$\ell_n\le \sum_{k=1}^nk!.$$ These bounds tell us that $152\le l_5\le 153$, but it has been shown in 2014 that in fact $\ell_5=153$. The next case seems to be still open: The inequalities only tell us $867\le \ell_6\le 873$, but another result of 2014 shows that $\ell_6\le 872$.