View Single Post
Factorial problem.
Old
DrJim
Guest
 
Status:
Posts: n/a
Lightbulb Factorial problem. - 12-10-2006, 08:07 PM

This one is a bit hard to give hints on without spelling out the solution - also a solution would be a bit hard to memorize even if I did - ironically because it would be so brief that it would also be very easy to goof up.

What I do if for some reason I need to calculate a factorial - and don't have a calculator with that function programmed in - is to just run through the calculation of the first few factorials by hand and then write a quick program duplicating the hand approach. The attached spreadsheet shows one example - note that factorials get large very quickly! (It was done in Works but you should be able to open it in Excel and most other spreadsheet programs.)

My first suggestion is that you do a similar calculation by hand and then try to figure out how to do it with a loop and recursively. Note that with the recursive approach, any local variables will be reset each time the method calls itself - so if you want to use something in multiple executions of a method, it has to be a world level variable.

Second, make sure to remember that the factorial of a number is simply the product of all integers up to and including that number. Hence
1! = 1
2! = 1 x 2 = 1! x 2 = 2
3! = (1 x 2) x 3 = 2! x 3 = 6
4! = (1 x 2 x 3) x 4 = 3! x 4 = 24
etc.

Hope this helps. Remember that while problems involving factorials can be difficult , calculating the value of a factorial itself is very simple.
Attached Files
File Type: zip factorials.zip (2.2 KB, 408 views)
   
Reply With Quote