ebook img

6. Enumerating Anagrams / Computing Factorials PDF

16 Pages·2011·0.27 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview 6. Enumerating Anagrams / Computing Factorials

6. Enumerating Anagrams / Computing Factorials Robert Snapp [email protected] DepartmentofComputerScience UniversityofVermont RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 1/13 1 Enumeratinganagrams 2 ComputingfactorialswithDrRacket 3 Astronomicalnumbers RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 2/13 Anagrams: Review InCS32,thewordanagramsignifiesapermutation,orrearrangementofagroupof lettersorsymbols. ThusthelettersinthewordCAThassixdifferentanagrams: ACT ATC CAT CTA TAC TCA (NotethatonlytwooftheseareEnglishwords,theremainingfourarejibberish,but areneverthelessanagrams,byourdefinition.) Thenumberofdifferentanagramsofawordhavingndifferentlettersequals nŠDn(cid:2).n(cid:0)1/(cid:2).n(cid:0)2/(cid:2)(cid:1)(cid:1)(cid:1)(cid:2)3(cid:2)2(cid:2)1: ThenotationnŠiscalled“n-factorial.” RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 3/13 Anagrams: Review Iftheinitialwordhasrepeatedletters,thenthenumberofanagramsisreduced. Thus thewordMALLhas12anagrams: ALLM ALML AMLL LALM LAML LLAM LLMA LMAL LMLA MALL MLAL MLLA Withfourletters,wemightexpecttofind4Špermuations. Howeverthisnumber overcountsthecorrectvalue,becauseLLhasonlyoneanagram,nottwo. Thuswe needtodivide4Šby2Š. Thus 4Š 24 D D12: 2Š 2 RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 4/13 Anagrams: Review HowmanyanagramsareinthewordLULL? HowmanyareinTOOT? RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 5/13 Anagrams: Review Recall,thatawordconsistingofk differentletters,withr repetitionsofthefirst,r of 1 2 thesecond,:::,andr ofthek-thletter,has k .r Cr C(cid:1)(cid:1)(cid:1)Cr /Š 1 2 k : r Š r Š (cid:1)(cid:1)(cid:1) r Š 1 2 k uniqueanagrams. Thusthe11letterwordABRACADABRA,whichhas5As,2Bs, 1C,1D,and2Rs,has 11Š 11(cid:2)10(cid:2)9(cid:2)8(cid:2)7(cid:2)6(cid:2)5Š D (Cancelcommonfactors!) 5Š (cid:1)2Š (cid:1)1Š (cid:1)1Š (cid:1)2Š 5Š(cid:1)2(cid:1)2 D11(cid:2)10(cid:2)9(cid:2)2(cid:2)7(cid:2)6 D99(cid:2)840D83;160 uniqueanagrams. (Wheneveronehasafractionwithfactorialsappearinginboththe numeratoranddenominator,onecanalwayscancelcommonfactors.) RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 6/13 Computing Factorials Computinglargefactorialsthoughisdifficultandtimeconsuming,evenwitha calculator. FortunatelywecanletDrRacketdothehardworkforus. Firstthough let’sexaminethemathematicaldefinitionofthefactorial: Foranynaturalnumber1 n ( 1; ifnD0; nŠD n(cid:2).n(cid:0)1/Š; otherwise. Thestatementthat0ŠD1isausefulconvention. Itmightsoundoddthatthereis exactlyonewaytopermuteananagramthathaszeroletters. However,0ŠD10,as bothareproductsthatcontainzerofactors. Andsince,10 D1,itfollowsthat0ŠD1. ThestatementthatnŠDn(cid:2).n(cid:0)1/Š,followsfromobservingthatinsideevery factorial,e.g.,5ŠD5(cid:2)4(cid:2)3(cid:2)2(cid:2)1,thereisaslightlysmallerone: Since 4ŠD4(cid:2)3(cid:2)2(cid:2)1,wehave5ŠD5(cid:2)4Š. 1Anaturalnumberisanon-negativeinteger RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 7/13 Computing factorials in DrRacket Inthetop(Definition)window,enterthefollowingfunctiondefinition. (define (factorial n) ( if (= n 0) 1 ((cid:3) n (factorial ((cid:0) n 1))))) Savethedefinitioninafilecalledfactorial.rkt. Thespecialform“define”enables ustodefinenewfunctionsinRacket. Don’tworrythatyoudon’tunderstandthe syntaxrightaway;ittakessomegettingusedto. Canyouobserveanysimilarity betweenthisfunction,andthemathematicaldefinitiononthepreviousslide? Pressthe“Run”button(withthegreenrunningstickfigure)ontheupperright. Then youcanevaluateexpressionslike (factorial 7) or (factorial 52) inthelower“Interaction”window. Justtypeinanexpressionafterthecommand prompt“>”andpressthereturnkeyonthekeyboard. RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 8/13 DrRacket on a Macintosh RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 9/13 Other ways to express the factorial Thepreviousdefinitionoffactorialisconcise. Theindentationsandlinebreaksare forourbenefit: DrRacketignoresthem. Wecanalsoaddcomments(whichbegin withasemicolon)tohelpremindushowthisfunctionworks. Wecanalsointroduce otherhelperfunctionsifwelike: ;;; dec stands for decrement; it takes one away ;;; from its argument. So, (dec 3) => 2. (define (dec n) ((cid:0) n 1)) ;;; ! returns the factorial of its argument, so (! 7) => 5040. (define (! n) ( if (zero? n) 1 ; 0! = 1, otherwise ((cid:3) n (! (dec n))))) ; n! = n (cid:3) (n(cid:0)1)! Thefunctionzero? isalreadypartofracket: (zero? 0)=>#t,i.e.. “true,”and(zero? 1) =>#f,thatis,“false.” TrycopyingtheaboveintotheDefinitionwindowinDrRacket,andrunafewtests. RobertR.Snapp©2011 6.EnumeratingAnagrams/ComputingFactorials CS21,Fall2011 10/13

Description:
Anagrams: Review. In CS 32, the word anagram signifies a permutation, or rearrangement of a group of letters or symbols. Thus the letters in the word
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.