There are many complex math functions that flash does not directly provide and factorials are one of them. Factorials are basically a way of taking the total product of all the numbers smaller than it. For example 3 Factorial (3!) is 3*2*1. You can use Factorials in permutations and combinations and also Taylor Series.
There are two exceptions to this function:
- 0 Factorial is 1
- You can't take the factorial of a negative number

Instead of the standard use of recursive functions (where a function calls itself over and over) this one simply loops until it reaches one and multiplies it to the total. Here's the basic source code:
If ( n > 0) Set Variable: "i" = 1 Set Variable: "total" = n Loop While (i<n) Set Variable: "total" = total*(n-i) Set Variable: "i" = i+1 End Loop Else If (n=0) Set Variable: "total" = 1 End If If (n<0) Set Variable: "total" = "Negative Num" End If
It first checks to see if n (the input number) is positive then it just decreases the number to be multiplied. If it's zero then the answer is automatically 1. It prints an error message.
To use in your own apps, either copy the above script into the appropriate frame or you can check out the button action in my .fla. Good luck and enjoy!
discuss this topic to forum
