Вы находитесь на странице: 1из 16

6.

Enumerating Anagrams / Computing Factorials Robert Snapp


snapp@cs.uvm.edu
Department of Computer Science University of Vermont

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

1 / 13

Enumerating anagrams

Computing factorials with DrRacket

Astronomical numbers

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

2 / 13

Anagrams: Review
In CS 32, the word anagram signies a permutation, or rearrangement of a group of letters or symbols. Thus the letters in the word CAT has six different anagrams: ACT CAT TAC ATC CTA TCA

(Note that only two of these are English words, the remaining four are jibberish, but are nevertheless anagrams, by our denition.) The number of different anagrams of a word having n different letters equals

n D n

.n

1/

.n

2/

1:

The notation n is called n-factorial.

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

3 / 13

Anagrams: Review
If the initial word has repeated letters, then the number of anagrams is reduced. Thus the word MALL has 12 anagrams:

ALLM LALM MALL

ALML LAML MLAL

AMLL LLAM MLLA

LLMA

LMAL

LMLA

With four letters, we might expect to nd 4 permuations. However this number overcounts the correct value, because LL has only one anagram, not two. Thus we need to divide 4 by 2. Thus

4 24 D D 12: 2 2

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

4 / 13

Anagrams: Review
How many anagrams are in the word LULL?

How many are in TOOT?

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

5 / 13

Anagrams: Review
Recall, that a word consisting of k different letters, with r1 repetitions of the rst, r2 of the second, : : :, and rk of the k -th letter, has

.r1 C r2 C r1 r2

C rk / : rk

unique anagrams. Thus the 11 letter word ABRACADABRA, which has 5 As, 2 Bs, 1 C, 1 D, and 2 Rs, has

11 11 D 5 2 1 1 2 D 11 D 99

10 10

9 8 7 5 2 2 9 2 7

6 6

(Cancel common factors!)

840 D 83;160

unique anagrams. (Whenever one has a fraction with factorials appearing in both the numerator and denominator, one can always cancel common factors.)

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

6 / 13

Computing Factorials
Computing large factorials though is difcult and time consuming, even with a calculator. Fortunately we can let DrRacket do the hard work for us. First though lets examine the mathematical denition of the factorial: For any natural number1 n

( n D

1; n .n

if n D 0;

1/; otherwise.

The statement that 0 D 1 is a useful convention. It might sound odd that there is exactly one way to permute an anagram that has zero letters. However, 0 D 10 , as both are products that contain zero factors. And since, 10 D 1, it follows that 0 D 1. The statement that n D n .n 1/, follows from observing that inside every factorial, e.g., 5 D 5 4 3 2 1, there is a slightly smaller one: Since 4 D 4 3 2 1, we have 5 D 5 4.

A natural number is a non-negative integer


Robert R. Snapp 2011 6. Enumerating Anagrams / Computing Factorials CS 21, Fall 2011 7 / 13

Computing factorials in DrRacket


In the top (Denition) window, enter the following function denition. ( define ( f a c t o r i a l n) ( i f (= n 0 ) 1 ( n ( factorial (

n 1)))))

Save the denition in a le called factorial.rkt. The special form dene enables us to dene new functions in Racket. Dont worry that you dont understand the syntax right away; it takes some getting used to. Can you observe any similarity between this function, and the mathematical denition on the previous slide? Press the Run button (with the green running stick gure) on the upper right. Then you can evaluate expressions like

(factorial 7)

or

(factorial 52)

in the lower Interaction window. Just type in an expression after the command prompt > and press the return key on the keyboard.

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

8 / 13

DrRacket on a Macintosh

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

9 / 13

Other ways to express the factorial


The previous denition of factorial is concise. The indentations and line breaks are for our benet: DrRacket ignores them. We can also add comments (which begin with a semicolon) to help remind us how this function works. We can also introduce other helper functions if we like: ; ; ; dec stands f o r decrement ; i t t a k e s one away ; ; ; from i t s argument . So , ( dec 3 ) => 2 . ( d e f i n e ( dec n ) ( n 1)) ; ; ; ! r e t u r n s t h e f a c t o r i a l o f i t s argument , so ( ! 7 ) => 5040. ( define ( ! n) ( i f ( zero ? n ) 1 ; 0! = 1 , otherwise ( n ( ! ( dec n ) ) ) ) ) ; n! = n ( n 1)! The function zero? is already part of racket: (zero? 0) => #t, i.e.. true, and (zero? 1) => #f, that is, false. Try copying the above into the Denition window in DrRacket, and run a few tests.
Robert R. Snapp 2011 6. Enumerating Anagrams / Computing Factorials CS 21, Fall 2011 10 / 13

How many ways can one shufe a deck of playing cards?

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

11 / 13

How many ways can one shufe a deck of playing cards?


By the multiplication principle there are 52 choices for the rst card, 51 for the second, 50 for the third, : : :

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

11 / 13

How many ways can one shufe a deck of playing cards?


By the multiplication principle there are 52 choices for the rst card, 51 for the second, 50 for the third, : : :

# of shufes D 52

D 52 51 50 D 52

3 2 1

D 80; 658; 175; 170; 943; 878; 571; 660; 636; 856; 403; 766; 975; 289; 505; 440; 883; 277; 824; 000; 000; 000; 000 80 1066

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

11 / 13

How many ways can one shufe a deck of playing cards?


By the multiplication principle there are 52 choices for the rst card, 51 for the second, 50 for the third, : : :

# of shufes D 52

D 52 51 50 D 52

3 2 1

D 80; 658; 175; 170; 943; 878; 571; 660; 636; 856; 403; 766; 975; 289; 505; 440; 883; 277; 824; 000; 000; 000; 000 80 1066

Outrageous Claim: This number is so large that the order of the cards in every well shufed deck has almost certainly never been realized before in the history of the universe!

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

11 / 13

Some other huge numbers


Number of seconds in a century 4:5 109 . Number of human beings alive on Earth 6:7 109 . Number of Oreo cookies sold between 1912 and 2007 4:9 1011 . 12 Federal debt (est. in US$ on 10/16/2010) 13:5 10 . Number of seconds that have elapsed since the Big Bang 4 1017 : Number of distinct positions in a 3 3 3 Rubiks cube 4 1019 . 21 Number of grains of sand on the earth 10 . Number of stars in the visible universe (Simon Driver, 2003) 7 1022 . 28 Number of atoms in a human body 10 . Mass of the sun (in kilograms) 1030 . Number of legal chess positions 1040 . Number of distinct positions in a 4 4 4 Rubiks Revenge 1045 . 67 Number of permutations of 52 playing cards 8 10 . Number of distinct positions in a 5 5 5 Rubiks Ultimate Cube 1074 . Number of physical particles in the universe (inationary model) 1080 . 768 Number of distinguishable games of go 10

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

12 / 13

Imagine this : : :
Imagine if every star in the universe has a life supporting planet that has been continuously populated with 7 billion inhabitants who have been shufing 52-card decks of playing cards once every second since the universe was created. This would account for

.7

1022 planets/

.7

109 persons/planet/

.4

1017 shufes/person/ D2 1050 shufes:

This equals only

1050 D 2:5 10 18 ; 1067 as a fraction of the total number of 52-card deck orderings. Thus the probability that a 2 8
well shufed deck has been realized before in the history of the universe is essentially 0.

Robert R. Snapp 2011

6. Enumerating Anagrams / Computing Factorials

CS 21, Fall 2011

13 / 13

Вам также может понравиться