'********************************************************************

'*

'* Function Factorial

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2004-12-21

'* Modified: 2004-12-21

'*

'* Purpose: Return the Factorial result for the number specifed.

'*            Factorial is the product of all positive integers less

'*            than or equal to anyNum.

'*

'* Input: anyNum = The number to calculate Factorial for.

'*

'* Output: Returns the Factorial result for the number specifed.

'*

'********************************************************************

Function Factorial(ByVal anyNum)

  'Version 1.0 2004-12-21

  'Non-recursive factorial for

  '-170< anyNum >170

  Dim i

  If anyNum < -170 Or anyNum > 170 Then

    Factorial = 0

    Exit Function

  End If

 

  If Abs(anyNum) <> anyNum Then

    Factorial = -1

    For i = -2 To anyNum Step -1

      Factorial = Factorial * i

    Next 'i

  Else

    Factorial = 1

    For i = 2 To anyNum

      Factorial = Factorial * i

    Next 'i

  End If 'Abs(anyNum) <> anyNum

End Function 'Factorial

 

 

 

'********************************************************************

'*

'* Function RecursiveFactorial

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2004-12-21

'* Modified: 2004-12-21

'*

'* Purpose: Return the Factorial result for the number specifed.

'*            Factorial is the product of all positive integers less

'*            than or equal to anyNum.

'*

'* Input: anyNum = The number to calculate Factorial for. 

'*

'* Output: Returns the Factorial result for the number specifed.

'*

'********************************************************************

Function RecursiveFactorial(anyNum)

  'Version 1.0 2004-12-21

  'Recursive Factorial for

  '-170< anyNum >170

  If anyNum < -170 Or anyNum > 170 Then

    RecursiveFactorial = 0

    Exit Function

  ElseIf anyNum = 0 Then

    RecursiveFactorial = 1

    Exit Function

  End If 'anyNum < -170 Or anyNum > 170

 

  If Abs(anyNum) <> anyNum Then

    RecursiveFactorial = anyNum * RecursiveFactorial(anyNum+1)

  Else

    RecursiveFactorial = anyNum * RecursiveFactorial(anyNum-1)

  End If 'Abs(anyNum) <> anyNum

End Function 'RecursiveFactorial

Article ID: 390, Created On: 9/25/2011, Modified: 9/25/2011