• home
  • forum
  • my
  • kt
  • download
  • Calculating Factorials in Flash

    Author: 2007-06-05 16:28:53 From:

    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

    relation tutorial

    No information

    Category

      3D (36)
      Math Physics (18)
      3rd Party (10)
      Navigation (70)
      Actionscripting (228)
      Optimization (17)
      Animation (166)
      Projector (11)
      Audio (54)
      Special Effects (170)
      Backend (26)
      Text Effects (92)
      Drawing (34)
      Tips and Techniques (58)
      Dynamic Content (38)
      Tricks (8)
      Games (114)
      Utilities (24)
      Getting Started (99)
      Video (59)
      Interactivity (48)
      Web Design (37)

    New

    Hot