Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type.
Why is this?
You can use factorial on integers (that should be the input), the output could be a string or a double, anyway you will alwas have the factorial. The only restriction is in the input.
Yes, i mean this function works fine for values up to 170! or something like that:
- Public Function Factorial(ByVal bNum As Integer) As Double
-
Dim I As Double
-
If bNum <= 0 Then Exit Function
-
Factorial = 1
-
For I = 1 To bNum
-
Factorial = Factorial * I
-
Next
-
End Function
Also VB can be used for making a little bit more complex functions like a Gamma.
What is a Gamma?
A Gamma Function is the generalization of the Factorial Function (this means you can use any positive number in it). If you use a Gamma for an integer, it'll give you the Factorial of the previus integer. i.e.:
G(5) = 4 ! = 24
The advantage is that even if you cannot have 4.25 ! , you can always have
G(5.25) = 35.2088612458766
Cool!!!
To obtain it, you can use many numerical methods. Here I used an old friend by Euler-Mascheroni.
- Public Function Gamma(ByVal Dou1 As Double) As Double
-
Dim I As Integer
-
Dim Dou2 As Double
-
Dim Dou3 As Double
-
-
If Fix(Dou1) = Dou1 Then
-
If Dou1 = 0 Or Dou1 = 1 Then
-
Gamma = 1
-
Exit Function
-
Else
-
Dou2 = 1
-
For I = 1 To Dou1 - 1
-
Dou2 = Dou2 * I
-
Next
-
Gamma = Dou2
-
Exit Function
-
End If
-
ElseIf Dou1 > 3 Then
-
Dou2 = 1
-
For I = 1 To (Fix(Dou1) - 1)
-
Dou2 = Dou2 * (Dou1 - 1)
-
Dou1 = Dou1 - 1
-
Next
-
Dou3 = Exp(-0.57721566 * Dou1) / Dou1
-
For I = 1 To 9999
-
Dou3 = Dou3 * ((1 + (Dou1 / I)) ^ (-1)) * Exp(Dou1 / I)
-
Next
-
Dou3 = Dou2 * Dou3
-
Gamma = Dou3
-
Else
-
Dou3 = Exp(-0.577215664901533 * Dou1) / Dou1
-
For I = 1 To 9999
-
Dou3 = Dou3 * ((1 + (Dou1 / I)) ^ (-1)) * Exp(Dou1 / I)
-
Next
-
Gamma = Dou3
-
End If
-
End Function
I hope this could be of some use next time you're dealing with someones homework.
Kad