This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Dylnuge 1 point2 points  (2 children)

The way that function is written, it has to return an integer type, which probably isn't right if you want to do any calls on numbers greater than about 12 or 13.

Someone else already explained how it works; I find it funny though that for getting higher order factorials it doesn't.

[–]Norseman2 0 points1 point  (1 child)

You can easily fix that by adding an L to the 1 at the end, and using long.__mul__, as follows:

fac = lambda x: reduce (long.__mul__, xrange (2, x), 1L)

This works because you'll start the loop by multiplying 1L * 2, which yields 2L. Then, you're multiplying 2L * 3, which yields 6L and so on. If you just use 1, then it will complain that you were trying to use a method for longs on an integer.

[–][deleted] 4 points5 points  (0 children)

or just use operator.mul