Database (Recursion and Factorials)
I am currently working on a factorial procedure that figures out a factorial for a number and eventually I will store it in the mysql database. I am kind of getting caught up with this program below. I am confused how the program is executed with all the recursive direct calls to itself. Can somebody step me through this one slowly so I can see the flow of where a number gets passed all the way to the end where it returns the final number. I am getting hung up here the most where the actual recursion takes place I guess where I am getting lossed is where does the calculation take place and how does it take place. For instance in this example I pass 5 it then gets to the line below and evaluates value = 5 * factorial(5-1)- right there that tells me to call the function again and pass it 4 then when I get to the line right below me $value = 4 * factorial(4-1). I don't see where the actual calculation takes place 5 * 4 * 3 * 2 = 120. Everytime a number gets passed I feel that somehow what I have done before is gone. Can somebody walk me through this very carefully so I can see what is really going on. $value *= factorial($value - 1); Thanks #!/usr/bin/perl sub factorial { my $value = shift (@_); if ($value == 1) { return $value; } else{ $value *= factorial($value - 1); return $value; } } $result = factorial(5); print $result; - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
Re: Database (Recursion and Factorials)
On Mon, May 14, 2001 at 09:32:05AM -0400, lkeeton wrote: [snip] how does it take place. For instance in this example I pass 5 it then gets to the line below and evaluates value = 5 * factorial(5-1)- right there that tells me to call the function again and pass it 4 then when I get to the line right below me $value = 4 * factorial(4-1). I don't see where the actual calculation takes place 5 * 4 * 3 * 2 = 120. Everytime a number gets passed I feel that somehow what I have done before is gone. Can somebody walk me through this very carefully so I can see what is really going on. This is far from a MySQL question. Try a perl forum, or any algorithm's manual. You need to understand the basics of recursion before you can understand that bit of code. Greetz, Peter. - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail [EMAIL PROTECTED] Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php