udfFactorial
udfPermutation
udfCombination
int udfFactorial (int)
int udfPermutation (int, int)
int udfCombination (int, int)
; Factorial, Permutation, Combination.
;
; Detlev Dalitz.20090405.Recoded.
; Original code maybe from Stan Littlefield, 2001.

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFactorial (intNumber)
Terminate (intNumber > 100 || intNumber < 1, "Terminated.", StrCat ("udfFactorial (intNumber)", @LF, "Valid number range: 1..100."))
If intNumber > 1 Then Return (1.0 * intNumber * udfFactorial (intNumber - 1))
Return 1
;..........................................................................................................................................
; A collection of n different items can be arranged in order Factorial(n) different ways.
; Note: Winbatch cannot go more than 100 levels deep.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfPermutation (selectcount, availablecount)
Return udfFactorial (availablecount) / udfFactorial (availablecount - selectcount)
;..........................................................................................................................................
; Permutation
; Given the following conditions are met:
; - each item in the group of items is unique
; - no repetition of the same item in the selection is allowed
; - even if a selection consists of the same items,
;   if order is different, we count it as a different selection
;
; When selecting "selectcount" items from "availablecount" available items,
; the number of possible sequences is expressed as permutation.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfCombination (selectcount, availablecount)
Return udfFactorial (availablecount) / (udfFactorial (availablecount - selectcount) * udfFactorial (selectcount))
;..........................................................................................................................................
; Combination
; When selecting "selectcount" items from "availablecount" available items,
; the number of possible combinations is expressed as combinations:
; Note that combinations are similar to permutations except that there is
; Factorial(selectcount) dividing the permutation.
; Different orderings of the same set of selection is considered the same selection.
; Combinations result in smaller numbers than permutation.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;Test

Message ("Factorial 4", udfFactorial (4))             ; 24
Message ("Permutation (4, 8)", udfPermutation (4, 8)) ; 1680
Message ("Combination (4, 8)", udfCombination (4, 8)) ; 70

Exit