IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Description of Objects in VPython

The factorial and combin Functions

from visual import *

from visual.factorial import *

factorial(N) = N!

combin(a,b) = a!/(b!*(a-b)!)

Note: To avoid confusion between the module named "factorial" and the function named "factorial", import the factorial module after importing the visual module itself.

A major use of these functions is in calculating the number of ways of arranging a group of objects. For example, if there are 5 numbered balls in a sack, there are factorial(5) = 5! = 5*4*3*2*1 = 120 ways of taking them sequentially out of the sack (5 possibilities for the first ball, 4 for the next, and so on).

If on the other hand the 5 balls are not numbered, but 2 are green and 3 are red, of the 120 ways of picking the balls there are 2! indistinguishable ways of arranging the green balls and 3! ways of arranging the red balls, so the number of different arrangements of the balls is combin(5,2) = 5!/(3!*2!) = 10.

Logically, the combin function is just a combination of factorial functions. However, cancellations in the numerator and denominator make it possible to evaluate the combin function for values of its arguments that would overflow the factorial function, due to the limited size of floating-point numbers. For example, combin(5,2) = 5!/(3!*2!) = (5*4)/2 = 10, and we didn't have to evaluate 5! fully.