Feed on

here’s a compact way of calculating factorials in python. in fact, since this method doesn’t rely on for or while loops and their ancillary overhead, it might be one of the faster ways of calculating a factorial.

let’s say you’d like to calculate k!

>>> reduce(lambda i,j : i*j, range(1,k+1))

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

11 Responses to “one line factorial in python”

  1. on 15 Mar 2007 at 8:31 pm Rickard Sandberg

    think you got a typo in the formula:
    the i in the range function should be one?

    reduce(lambda i,j : i*j, range(1,k+1))

  2. on 15 Mar 2007 at 8:34 pm Lawrence David

    thanks rickard – you were right about the typo!

  3. on 08 Oct 2008 at 12:56 am ploden

    Very nice! Just be aware that this doesn’t work for k=0.

  4. on 16 Feb 2009 at 10:59 am Nick_Greece

    Very nice and elegant!!!

  5. on 23 Feb 2009 at 10:10 pm Saprophyte

    Very cool one liner Lawrence. I just wanted to note that it can be easily made to work for the k=0 case by the following modification:

    reduce(lambda i,j: i*j, range(1,k+1),1)

  6. on 04 Apr 2009 at 1:01 am Rich

    You don’t need to lambda-define multiplication. :P How about reduce(int.__mul__, range(1, k+1))?

  7. on 24 Aug 2009 at 6:47 pm Arkapravo

    Rich ! ….. you are cool ! …. no lambda ….. keep it as python is to be done …. int._mul_ ….

    The correct function is ….

    >>>reduce(int.__mul__, range(1, k+1),1)

  8. on 14 Sep 2009 at 10:57 pm Manas Shaikh

    Yeah, that helped. Thanks!

  9. on 15 Sep 2009 at 9:40 am Yiannis Tsiouris

    By changing range(…) with xrange(…), ie:

    >>> reduce(int.__mul__, xrange(1, k+1), 1)

    someone can accomplish even (slightly) better performance code as far as looping and memory efficiency is concerned (see help(xrange) for details).

    Another “fix” would be to use the mul function from the operator module (import operator) so as not to have problems with the factorial of “big numbers”:

    >>>import operator
    >>> reduce(operator.mul, xrange(1, k+1))

    Here are some measures trying to compute 60! using timeit module:

    In [74]: timeit.Timer(‘reduce(lambda i,j: i*j, range(1, 61))’).timeit(number=100000)
    Out[74]: 2.4469389915466309

    In [75]: timeit.Timer(‘reduce(lambda i,j: i*j, xrange(1, 61))’).timeit(number=100000)
    Out[75]: 2.3416600227355957

    In [76]: timeit.Timer(‘import operator; reduce(operator.mul, xrange(1, 61))’).timeit(number=100000)
    Out[76]: 1.5750751495361328

  10. on 11 Jan 2012 at 6:18 pm Willem Wardenaar

    Why spend time on multiplying with one?

    So I would use:
    reduce(int.__mul__, xrange(2, k+1), 1)

    I actually are using:

    from operator import mul
    fact = lambda k: reduce( mul, range( 2, k + 1 ), 1 )
    fact( 61 )

  11. on 11 Jan 2012 at 6:28 pm Willem Wardenaar

    Can anybody change the line “I actually are using:” into “I am actually using:” in my previous entry and remove this entry?

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.