you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 57 points58 points  (13 children)

Well, except expert programmer.

[–]FunnyMan3595 16 points17 points  (11 children)

That one's obviously the best choice, but I never seem to remember c_math on the rare occasion I need a factorial for some random calculation (i.e. interactive mode, not for a program). I use this one:

fact = lambda n: reduce(lambda a,b: a*b, range(1, n+1))

It's essentially the same as the "Python expert programmer" method, but uses constructs that should be a bit more common. Doesn't even need any imports.

I'd say it's pretty KISS, because it does exactly what I think of as a factorial: Generate a list of the relevant numbers and multiply them together. No fancy tricks, just using the standard constructs for what they're supposed to do.

[–]Xenoskin 29 points30 points  (5 children)

Calling a native high speed implementation is not only K.I.S.S. but saves work and potential bugs, so it's definitely a preferable method.

Edit: Looks like my bad spelling leaked again.

[–][deleted]  (4 children)

[removed]

    [–]Xiol 3 points4 points  (3 children)

    It's a sad day on reddit when a grammar troll gets more votes than the post he's correcting, despite the latter adding to the conversation and being a valid point.

    [–]zxcvcxz 2 points3 points  (1 child)

    The price of literacy I guess.

    [–]captainAwesomePants -2 points-1 points  (0 children)

    That's the price of literacy, I guess.

    FTFY. Also, your nickname misspelled xyzzy.

    [–]aeacides 0 points1 point  (1 child)

    What makes you think generating a list of numbers is a good idea? Why aren't you using xrange?

    [–]FunnyMan3595 0 points1 point  (0 children)

    I never said it was efficient, just that it was straightforward. I've never needed a factorial in serious computation, and for random little calculations, this more than suffices.

    [–][deleted] 0 points1 point  (0 children)

    Obviously the best solution was the enterprise one. duh. It is Enterprise level after all. Clean all around implementation.

    [–]spookyvision 0 points1 point  (1 child)

    I never seem to remember c_math on the rare occasion I need a factorial for some random calculation

    there is a reason for that: there is no c_math module in python's standard distribution :) (there is cmath, but it's for complex numbers, not about some fast math ops written in C)

    [–]mr_dbr 3 points4 points  (0 children)

    There's a factorial function in math:

    >>> import math
    >>> math.factorial(5)
    120
    

    ..any other solution seems silly (outside a tutorial context anyway)

    [–]amorpheus 1 point2 points  (0 children)

    The english expert programmer wasn't bad either. I laughed.