Scriptorium

us fr nl




VBA : Math: Two functions to calculate a factorium (n!)   us


Options: Save as PDF | Save attached file | Toggle line numbers

Details:

Type: function
Added By: Rembo
Short Description:
These are two functions to calculate a factorium. The first functions works with a loop, the second is a recursive function (calls itself). 
Notes:
It's easier to use a worksheet function in VBA (Application.WorksheetFunction...) but this is just a programming solution.
Added: Jul 15 2005 at 4:01 PM
Modified: Sep 22 2005 at 2:23 PM
Related URLs


Usage:

Call the function as a User Defined Function (UDF) and supply a integer as
parameter. E.g. =Factorum(3) of Factorium2(3) (will give you answer '6')


Code:

Formatted | Unformatted
  1. Function Factorial(n As Integer) As Double
  2. ' Function with loop to calculate a factorial
  3. Factorial = 1
  4. For i = 1 To n
  5. Factorial = Factorial * i
  6. Next i
  7. End Function
  8.  
  9.  
  10.  
  11. Function Factorial2(n As Integer) As Double
  12. ' Recursive function to calculate a factorial
  13. If n < 1 Then
  14. Factorial2 = 1
  15. Else
  16. Factorial2 = n * Factorial2(n - 1)
  17. End If
  18. End Function



User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website