Possible Duplicate:
Need help in building efficient exhaustive search algorithm
Imagine that you must open a locked door by inputting the correct 4-digit code on a keypad. After every keypress the lock evaluates the sequence of the last 4 digits inputted, i.e. by entering 123456 you have evaluated 3 codes: 1234, 2345 and 3456.
- What is the shortest sequence of keypresses to evaluate all
10^4different combinations? - Is there a method for traversing the entire space easy enough for a human to follow?
I have pondered this from time to time since a friend of mine had to brute force such a lock, to not having to spend the night outdoors in wintertime.
My feeble attempts at wrapping my head around it
With a code of length L=4 digits and an “alphabet” of digits of size D=10 the length of the optimal sequence cannot be shorter than D^L + L - 1. In simulations of smaller size than [L,D] = [4,10] I have obtained optimal results by semi-randomly searching the space. However I do not know if a solution exists for an arbitrary [L,D] pair and would not be able to remember the solution if I ever had to use it.
Lessons learned so far
When planning to spend the night at a friends house in another town, be sure to not arrive at 1 am if that person is going out to party and won’t hear her cell phone.
The link Evgeny provided should answer both of your quests. This answer is a bit offtopic, but you ask for a solution for humans.
In the real world you should probably rely more on Social engineering or heuristics, and after that on mathematics. I give a case on real life:
I went to visit an apartment and I found out that my cellphone was dead. Now way of contacting the person doing the visit. I was about to go back when I saw that the door used a keypad
0 - 9andA B. I made several assumptions:AorB(based on my own building).Awas used,Bnot used )At the end I had 3 numbers which were in the code for sure, 2 candidates for the last number and I was sure
Awas at the end. On key was just slightly damaged compared to the others.I just had to enumerate permutations starting with the candidate which seemed the more damaged, which give me
4! + 4! = 48tries. Believe me, at the 5th try the door was opened. If I can give my 2 cents, the oldput a key and open the dooris still the most reliable method to restrict access to a building.