Factorial Fun
May 28, 2007 · by Scott Fradkin
I just got back from Narglethorpe; at least I’m pretty sure it wasn’t a dream. One of the most spectacular animals on the island is called “The Beast of Narglethorpe” or TBON. Now, local legend has it that TBON has some interesting properties. If a person were to come upon this ferocious beast and call out a number, the beast will undergo a miraculous transformation in which it splits up into numerous other (albeit smaller) TBONs based on the number called out. Facing the possibility of being eaten by the beast, I decided that there was scientific merit in trying to verify the legend. So, armed with a machete, some camouflage, and a camera, I wandered into the jungles of Narglethorpe. The foliage was so dense that it was quite dark, but this did not deter me in the slightest. I went further and further into the jungle hoping to catch a glimmer of this monster. After a bit I heard a noise to my left. I turned, but saw nothing. I went on. Soon, I came to a clearing. I got to the middle of the clearing when I felt as if I were being watched. I turned around slowly and I saw that most amazing sight Ii had ever seen! Before me was the magnificent TBON!
Ten feet tall! Giant floppy ears! A cute puffball of a tail!
Terror inched across my body. I was paralyzed with fear. Until I remembered… “One!”, I managed to blurt out. Nothing happened. I thought for sure I was a goner. “Two!”, I shouted. The fearsome TBON split into two identical TBONs before my eyes. Identical beings, but each half the size of the original. Within a few seconds the two turned back into one.
Its eyes gleamed with hunger. Not undaunted, I cried, “Three!”. This time six smaller copies sprang forth. I was beginning to have an inkling as to what was going on. After some time, but longer than before, the six turned back into one. I quickly called out “Four!” Lo and behold, 24 copies appeared. I was quite excited and was ready to make things interesting. I quickly wrote down the following:
public class Factorial { public static void main(String[] args) { System.out.println(fact(new Integer(args[0]).intValue()); }public static int fact(int factnum) { switch(factnum) { case 0: return 1;default: return factnum * fact(factnum " 1); } } }
Nice and easy recursion. Something about this seems a bit pedestrian, however. What if we tried this a different way?
if ARGV[0].to_i == 0
puts "1"
else
puts((1..ARGV[0].to_i).inject(1) {|total, num| total * num})
endNice and concise.
Another just for kicks:
(begin
(define fact
(lambda (n)
(if (= n 0)
1<br>
(* n (fact (- n 1))))))
(display (fact (string->number (vector-ref argv 0))))
(newline))Ahh… Very helpful. Three different ways to calculate a factorial in three different languages.
I figured that I didn’t have to go past the number five. Any more than that and I would not be able to see the TBONs that split. So I called out “Five!”. As the TBON split into 120 identical (much) smaller TBONs, I broke out into a huge grin. I thought, “This TBON isn’t too scary.” I pet a couple of the TBON clones. After a few minutes, the TBON reassembled itself into the regular, giant TBON. It glared at me again. I became afraid once more.
“Hey!”, said a voice behind me. I did a little jump and turned around slowly. A man stood behind me.
“He really won’t hurt you, you know.”, the man said.
“Oh, really?”, I answered. “He looks quite angry to me.”
“Nah, he’s harmless. I’ve lived here all my life and the Beast has never harmed a soul. He’s looking at you like that because of what you’ve got in your pocket.”
“My pocket?” Oh, how embarrassed I was.
I grinned again, reached into my pocket and pulled out the carrot and tossed to the TBON. The TBON caught the carrot, wiggled its nose, then bounced off back into the jungle. I turned around said thanks to the local and decided it was time to head home.
Authors note: The code samples in this post are all executable. The first is in Java, the second is in Ruby, and the third is in Scheme. They are setup to take a command line argument of the factorial to compute. Save the Java example to a file called factorial.java, compile it using "javac factorial.java" and execute it using "java Factorial n" where 'n' is the factorial to compute. Save the Ruby example to a file and run it using "ruby file.rb n". The Scheme example should be saved to a file and run using (I use MzScheme) "mzscheme -r file.scm n".
Filed in: Team Member Blog Comments (0)

Comments
There are no comments for this entry.
Commenting is not available in this section entry.