Hello, I feel this is the right place for having methods that also counts the number of permutations/combinations to generate, or having generated. Actually, the methods I posted above are pretty much dependent on those, so why not have them in the same thread.
The factorial handler, tells how many permutations you get out of a set of n objects.
The C handler (or binomial coeffecient) tells how many r combinations you get out a set of n elements. Another way to say this is, how many ways can I generate a subset with r elements, when order doesn’t matter, out of a set with n elements.
The P handler tells how many r permutations out of a set of n elements you can generate. (subsets with r elements out of a set of n elements).
on factorial(n)
if n > 170 then error "Factorial: n too large."
# Result greater than 1.797693E+308!
if n < 0 then error "Factorial: n not positive."
set a to 1
repeat with i from 1 to n
set a to a * i
end repeat
return a
end factorial
on C(n, r)
# Counts the number of r-combinations in a set of n elements.
# it is also called the binomial coeffecient.
if n = 0 or n < r or r < 0 then error "C: argument error"
return (factorial(n) div (factorial(r) * (factorial(n - r))))
end C
on P(n, r)
# counts the number of r-permutations in a set of n elements.
if n = 0 or n < r or r < 0 then error "P: argument error"
return (factorial(n) div (factorial(n - r)))
end P
Edit
Made boundary tests, slightly more correct, as r is now allowed to be zero.