WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Math Related UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Factorials Combination Permutations

; 4 committees -- 5 people 
; Permutation: how many possible committees, 
; Combination: how many with no one person on more than 1.
; Factorial:   how many ways they can sit at a table


;A collection of n different items can be arranged
;in order Factorial(n) different ways.
#DefineFunction FactorialRecursive(x)
   ;Ooop  Winbatch canot go more than 100 levels deep
   ;so...
   Terminate(x>100,"Error","Number too large")
   ;And Factorial does not like negative numbers
   Terminate(x<1, "Error","number too small")
   if x == 1 then return(1)
   return( x * Factorial(x-1) +0.0 ) 
#EndFunction

;Truth be told, a little FOR loop is more efficient
;to compute factorials.
#DefineFunction Factorial(x)
   Terminate(x<1, "Error","number too small")
   ans=1.0
   for xx = 2 to x
      ans=ans * xx
   next
   return(ans)
#EndFunction


;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 permunation:

#DefineFunction Permutation(selectcount,availablecount)
   ans = Factorial(availablecount) / Factorial(availablecount-selectcount)
   return ans
#EndFunction

;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 permuation. Different
;  orderings of the same set of selection is considered the same selection.
;  Combinations result in smaller numbers than permutation.


#DefineFunction Combination(selectcount,availablecount)
   ans = Factorial(availablecount) / ( Factorial(availablecount-selectcount) * Factorial(selectcount) )  
   return ans
#EndFunction

Message("Factorial 4",Factorial(4))

Message("Permutation(4,8)",Permutation(4,8))

Message("Combination(4,8)",Combination(4,8))

Article ID:   W15011
File Created: 2001:11:08:12:41:20
Last Updated: 2001:11:08:12:41:20