Thursday, June 22, 2006

Martin visits the dragon again

‘‘Hello Dragon!’’ Martin called as he made his way down the rickety dungeon staircase.

‘‘Hmmmph! You again. I’m on to your recursive tricks.’’ The dragon did not sound glad to see him.

‘‘I’m supposed to find out what five factorial is,’’ Martin said. ‘‘What’s factorial mean, anyway?’’

At this the dragon put on a most offended air and said, ‘‘I’m not going to tell you. Look it up in a book.’’

‘‘All right,’’ said Martin. ‘‘Just tell me what five factorial is and I’ll leave you alone.’’

‘‘You don’t know what factorial means, but you want me to tell you what factorial of five is??? All right buster, I’ll tell you, not that it will do you any good. Factorial of five is five times factorial of four. I hope you’re satisfied. Don’t forget to bolt the door on your way out.’’

‘‘But what’s factorial of four?’’ asked Martin, not at all pleased with the dragon’s evasiveness.

‘‘Factorial of four? Why, it’s four times factorial of three, of course.’’

‘‘And I suppose you’re going to tell me that factorial of three is three times factorial of two,’’ Martin said.

‘‘What a clever boy you are!’’ said the dragon. ‘‘Now go away.’’

‘‘Not yet,’’ Martin replied. ‘‘Factorial of two is two times factorial of one.

Factorial of one is one times factorial of zero. Now what?’’

‘‘Factorial of zero is one,’’ said the dragon. ‘‘That’s really all you ever need to remember about factorials.’’

‘‘Hmmm,’’ said Martin. ‘‘There’s a pattern to this factorial function. Perhaps I should write down the steps I’ve gone through.’’ Here is what he wrote:

Factorial(5) = 5 x Factorial(4)
= 5 x 4 x Factorial(3)
= 5 x 4 x 3 x Factorial(2)
= 5 x 4 x 3 X 2 x Factorial(1)
= 5 x 4 x 3 x 2 x 1 x Factorial(0)
= 5 x 4 x 3 x 2 x 1 x 1

‘‘Well,’’ said the dragon, ‘‘you’ve recursed all the way down to factorial of zero, which you know is one. Now why don’t you try working your way back up to....’’

When it realized what it was doing, the dragon stopped in mid-sentence. Dragons aren’t supposed to be helpful.

Martin started to write again:
1 x 1= 1
2 x 1 x 1= 2
3 x 2 x 1 x 1= 6
4 x 3 x 2 x 1 x 1= 24
5 x 4 x 3 x 2 x 1 x 1= 120

‘‘Hey!’’ Martin yelped. ‘‘Factorial of 5 is 120. That’s the answer! Thanks!!’’

‘‘I didn’t tell you the answer,’’ the dragon said testily. ‘‘I only told you that factorial of zero is one, and factorial of n is n times factorial of n-1. You did the rest yourself. Recursively, I might add.’’

‘‘That’s true,’’ said Martin. ‘‘Now if I only knew what ‘recursively’ really meant.’’

No comments: