IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

The Visual Module of VPython - Reference Manual

Date de publication : 4 février 2009


factorial/combin


factorial/combin

The factorial and combin Functions

from visual import *
from visual.factorial import * # import after visual
print factorial(4) # gives 24 
print combin(10,2) # gives 45
Note: To avoid confusion between the module named "factorial" and the function named "factorial", import the factorial module after importing the visual module itself.

The factorial function factorial(N) is N!; 4! is (4)(3)(2)(1) = 24, and 0! is defined to be 1.
The combin function is combin(a,b) = a!/(b!*(a-b)!).

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.

 

Valid XHTML 1.1!Valid CSS!