Combination Cracking

  1. There is a keypad entry system on a safe door. There is no 'clear' button, but the safe will open automatically if the last n digits entered match the n digit 'password' required.
    1. If there are 10 digits to choose from, what is the fewest number of button presses you would need to type in to guarantee access if the password is 4 digits long?
    2. What is the length and general solution for a password of n digits?
    3. What about password length n, with m digits to choose from?
    4. Can you give an example sequences for (n,m) = (3,2), (3,3), (2,4)?
       
  2. A combination lock consists of 4 wheels, each numbered sequentially 0 to 9 (with 9 next to both 8 and 0.)  How many wheel turns (changing one digit by one value) are needed to attempt every combination? How many wheel turns are needed for n wheels of m digits each?

Source: Alan O'Donnell (though I've seen it elsewhere) with original extensions.


Solutions were received from Kirk Bresniker, Joseph DeVincentis, Dan Chirca, and Leendert Biemans.  Kirk's solution is below:

You can select the codes in order so that you enter the minimal length sequence, which would be the total number of unique combinations + the length of the code - 1. You first enter (code length) button presses.
This gets you one code. Every code press you enter after that gets you another combination, since you already have one, you'll need to hit another (total combinations - 1) codes until you're done.

Note that for 2 digits, this is Sloane's sequence A052944:
http://www.research.att.com/projects/OEIS?Anum=A052944

This is a well known problem, usually called the shortest common superstring problem. It is NP hard. It has very pratical applications in genome mapping, and there's lots of work on generating a common superstring that is no worse than (some limit)x as long as the idea string.

1.a. 10 digits, code length 4 -> 10^4 + 4 -1 = 10003 1.b. 10 digits, code length n -> 10^n + n - 1 1.c. m digits, code length n -> m^n + n - 1 1.d (3,2) -> 0001011100
(3,3) -> 00010020110120210221112122200
(4,2) -> 00102031121322330

Note that in these solutions, you could start at any point, and wrap through the n^m+n-1 presses.

2. Assuming that the knobs start at 0 0 0 0, and that I start with that number as the start of my sequence, then i'll need to turn the wheels
99,999 times (one less than 10 * the number of combinations)

I'll assert that for m digits on n wheels, we'll need m * (m^n) - 1 wheels turns.


Kirk also provided a 10003 digit solution to the first problem:
Generating all 10 digits arranged in length  4 codes = 10000 codes.
Done after 10003 presses: 00001000200030004000500060007000800090011001
2001300140015001600170018001900210022002300240025002600270028002900310
0320033003400350036003700380039004100420043004400450046004700480049005
1005200530054005500560057005800590061006200630064006500660067006800690
0710072007300740075007600770078007900810082008300840085008600870088008
9009100920093009400950096009700980099010102010301040105010601070108010
9011101120113011401150116011701180119012101220123012401250126012701280
1290131013201330134013501360137013801390141014201430144014501460147014
8014901510152015301540155015601570158015901610162016301640165016601670
1680169017101720173017401750176017701780179018101820183018401850186018
7018801890191019201930194019501960197019801990202030204020502060207020
8020902110212021302140215021602170218021902210222022302240225022602270
2280229023102320233023402350236023702380239024102420243024402450246024
7024802490251025202530254025502560257025802590261026202630264026502660
2670268026902710272027302740275027602770278027902810282028302840285028
6028702880289029102920293029402950296029702980299030304030503060307030
8030903110312031303140315031603170318031903210322032303240325032603270
3280329033103320333033403350336033703380339034103420343034403450346034
7034803490351035203530354035503560357035803590361036203630364036503660
3670368036903710372037303740375037603770378037903810382038303840385038
6038703880389039103920393039403950396039703980399040405040604070408040
9041104120413041404150416041704180419042104220423042404250426042704280
4290431043204330434043504360437043804390441044204430444044504460447044
8044904510452045304540455045604570458045904610462046304640465046604670
4680469047104720473047404750476047704780479048104820483048404850486048
7048804890491049204930494049504960497049804990505060507050805090511051
2051305140515051605170518051905210522052305240525052605270528052905310
5320533053405350536053705380539054105420543054405450546054705480549055
1055205530554055505560557055805590561056205630564056505660567056805690
5710572057305740575057605770578057905810582058305840585058605870588058
9059105920593059405950596059705980599060607060806090611061206130614061
5061606170618061906210622062306240625062606270628062906310632063306340
6350636063706380639064106420643064406450646064706480649065106520653065
4065506560657065806590661066206630664066506660667066806690671067206730
6740675067606770678067906810682068306840685068606870688068906910692069
3069406950696069706980699070708070907110712071307140715071607170718071
9072107220723072407250726072707280729073107320733073407350736073707380
7390741074207430744074507460747074807490751075207530754075507560757075
8075907610762076307640765076607670768076907710772077307740775077607770
7780779078107820783078407850786078707880789079107920793079407950796079
7079807990808090811081208130814081508160817081808190821082208230824082
5082608270828082908310832083308340835083608370838083908410842084308440
8450846084708480849085108520853085408550856085708580859086108620863086
4086508660867086808690871087208730874087508760877087808790881088208830
8840885088608870888088908910892089308940895089608970898089909091109120
9130914091509160917091809190921092209230924092509260927092809290931093
2093309340935093609370938093909410942094309440945094609470948094909510
9520953095409550956095709580959096109620963096409650966096709680969097
1097209730974097509760977097809790981098209830984098509860987098809890
9910992099309940995099609970998099911112111311141115111611171118111911
2211231124112511261127112811291132113311341135113611371138113911421143
1144114511461147114811491152115311541155115611571158115911621163116411
6511661167116811691172117311741175117611771178117911821183118411851186
1187118811891192119311941195119611971198119912121312141215121612171218
1219122212231224122512261227122812291232123312341235123612371238123912
4212431244124512461247124812491252125312541255125612571258125912621263
1264126512661267126812691272127312741275127612771278127912821283128412
8512861287128812891292129312941295129612971298129913131413151316131713
1813191322132313241325132613271328132913321333133413351336133713381339
1342134313441345134613471348134913521353135413551356135713581359136213
6313641365136613671368136913721373137413751376137713781379138213831384
1385138613871388138913921393139413951396139713981399141415141614171418
1419142214231424142514261427142814291432143314341435143614371438143914
4214431444144514461447144814491452145314541455145614571458145914621463
1464146514661467146814691472147314741475147614771478147914821483148414
8514861487148814891492149314941495149614971498149915151615171518151915
2215231524152515261527152815291532153315341535153615371538153915421543
1544154515461547154815491552155315541555155615571558155915621563156415
6515661567156815691572157315741575157615771578157915821583158415851586
1587158815891592159315941595159615971598159916161716181619162216231624
1625162616271628162916321633163416351636163716381639164216431644164516
4616471648164916521653165416551656165716581659166216631664166516661667
1668166916721673167416751676167716781679168216831684168516861687168816
8916921693169416951696169716981699171718171917221723172417251726172717
2817291732173317341735173617371738173917421743174417451746174717481749
1752175317541755175617571758175917621763176417651766176717681769177217
7317741775177617771778177917821783178417851786178717881789179217931794
1795179617971798179918181918221823182418251826182718281829183218331834
1835183618371838183918421843184418451846184718481849185218531854185518
5618571858185918621863186418651866186718681869187218731874187518761877
1878187918821883188418851886188718881889189218931894189518961897189818
9919192219231924192519261927192819291932193319341935193619371938193919
4219431944194519461947194819491952195319541955195619571958195919621963
1964196519661967196819691972197319741975197619771978197919821983198419
8519861987198819891992199319941995199619971998199922223222422252226222
7222822292233223422352236223722382239224322442245224622472248224922532
2542255225622572258225922632264226522662267226822692273227422752276227
7227822792283228422852286228722882289229322942295229622972298229923232
4232523262327232823292333233423352336233723382339234323442345234623472
3482349235323542355235623572358235923632364236523662367236823692373237
4237523762377237823792383238423852386238723882389239323942395239623972
3982399242425242624272428242924332434243524362437243824392443244424452
4462447244824492453245424552456245724582459246324642465246624672468246
9247324742475247624772478247924832484248524862487248824892493249424952
4962497249824992525262527252825292533253425352536253725382539254325442
5452546254725482549255325542555255625572558255925632564256525662567256
8256925732574257525762577257825792583258425852586258725882589259325942
5952596259725982599262627262826292633263426352636263726382639264326442
6452646264726482649265326542655265626572658265926632664266526662667266
8266926732674267526762677267826792683268426852686268726882689269326942
6952696269726982699272728272927332734273527362737273827392743274427452
7462747274827492753275427552756275727582759276327642765276627672768276
9277327742775277627772778277927832784278527862787278827892793279427952
7962797279827992828292833283428352836283728382839284328442845284628472
8482849285328542855285628572858285928632864286528662867286828692873287
4287528762877287828792883288428852886288728882889289328942895289628972
8982899292933293429352936293729382939294329442945294629472948294929532
9542955295629572958295929632964296529662967296829692973297429752976297
7297829792983298429852986298729882989299329942995299629972998299933334
3335333633373338333933443345334633473348334933543355335633573358335933
6433653366336733683369337433753376337733783379338433853386338733883389
3394339533963397339833993434353436343734383439344434453446344734483449
3454345534563457345834593464346534663467346834693474347534763477347834
7934843485348634873488348934943495349634973498349935353635373538353935
4435453546354735483549355435553556355735583559356435653566356735683569
3574357535763577357835793584358535863587358835893594359535963597359835
9936363736383639364436453646364736483649365436553656365736583659366436
6536663667366836693674367536763677367836793684368536863687368836893694
3695369636973698369937373837393744374537463747374837493754375537563757
3758375937643765376637673768376937743775377637773778377937843785378637
8737883789379437953796379737983799383839384438453846384738483849385438
5538563857385838593864386538663867386838693874387538763877387838793884
3885388638873888388938943895389638973898389939394439453946394739483949
3954395539563957395839593964396539663967396839693974397539763977397839
7939843985398639873988398939943995399639973998399944445444644474448444
9445544564457445844594465446644674468446944754476447744784479448544864
4874488448944954496449744984499454546454745484549455545564557455845594
5654566456745684569457545764577457845794585458645874588458945954596459
7459845994646474648464946554656465746584659466546664667466846694675467
6467746784679468546864687468846894695469646974698469947474847494755475
6475747584759476547664767476847694775477647774778477947854786478747884
7894795479647974798479948484948554856485748584859486548664867486848694
8754876487748784879488548864887488848894895489648974898489949495549564
9574958495949654966496749684969497549764977497849794985498649874988498
9499549964997499849995555655575558555955665567556855695576557755785579
5586558755885589559655975598559956565756585659566656675668566956765677
5678567956865687568856895696569756985699575758575957665767576857695776
5777577857795786578757885789579657975798579958585958665867586858695876
5877587858795886588758885889589658975898589959596659675968596959765977
5978597959865987598859895996599759985999666676668666966776678667966876
6886689669766986699676768676967776778677967876788678967976798679968686
9687768786879688768886889689768986899696977697869796987698869896997699
8699977778777977887789779877997878797888788978987899797988798979987999
8888988998989999000

Mail to Ken