Factorials, Permutations, Combinations
4
Utsawin
Go easy on me, my first snippet - plus I am new to VB.NET 2.0... with only basic knowledge of classic VB... Just want to mention that the Permutations function is not actually mine - found it online... (credit where credit's due)
Fairly simple stuff, but had me pulling my hair out - Even Excel has a COMBI function out of the box!
Fairly simple stuff, but had me pulling my hair out - Even Excel has a COMBI function out of the box!
Visual Basic
'***** Factorial / Permutations / & Finally Combinations *****
Public Function Factorial(ByVal factor As Double) As Double
If factor = 0 Then
Factorial = 1
Else
Factorial = factor * Factorial(factor - 1)
End If
Exit Function
Return (Factorial)
End Function
Public Function Permutations(ByVal a As Double, ByVal b As Double) As Double
' a!()
'a_P_b = --------
' (a - b)!
Permutations = Factorial(a) / Factorial(a - b)
Return (Permutations)
End Function
Public Function Combinations(ByVal a As Double, ByVal b As Integer) As Integer
' a_P_b
'a_C_b = --------
' b!
Combinations = Permutations(a, b) / Factorial(b)
End Function
'***** Factorial / Permutations / & Finally Combinations *****
Public Function Factorial(ByVal factor As Double) As Double
If factor = 0 Then
Factorial = 1
Else
Factorial = factor * Factorial(factor - 1)
End If
Exit Function
Return (Factorial)
End Function
Public Function Permutations(ByVal a As Double, ByVal b As Double) As Double
' a!()
'a_P_b = --------
' (a - b)!
Permutations = Factorial(a) / Factorial(a - b)
Return (Permutations)
End Function
Public Function Combinations(ByVal a As Double, ByVal b As Integer) As Integer
' a_P_b
'a_C_b = --------
' b!
Combinations = Permutations(a, b) / Factorial(b)
End Function






I only posted this as it took me hours searching and going through the MSDN library before I even realised it wasn't an included function... Just trying to help save some time for others...
Oh, and thx for the pointless ratings...
..:: It's better to look stupid, than open your mouth and remove all doubt ::..
http://www.dsc-services.be
ScriptLibrary
=================
Matthew R. Miller
http://bluecreststudios.com
http://www.codeandcoffee.com