Feature suggestion -- return if true

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Apr 19 02:35:39 CEST 2011


Chris Angelico wrote:

> Question: How many factorial functions are implemented because a
> program needs to know what n! is, and how many are implemented to
> demonstrate recursion (or to demonstrate the difference between
> iteration and recursion)? :)

A related question is how often factorial functions are even
*used* in real code.

I can think of two uses for factorials off the top of my
head:

* Coefficients of polynomials resulting from Taylor expansions.
In that case you probably have a loop that's calculating the
numerators and denominators iteratively, and the factorial
is folded into that. Or you're looking up the coefficients in
a table and not calculating factorials at all.

* Combinations and permutations -- again, the factorials are
probably folded into a loop that calculates the result in a
more direct and efficient way.

-- 
Greg



More information about the Python-list mailing list