4 ms·
How about: from math import prod # from Python 3.8 onwards def factorial(n): return prod(range(n+1))
by asplake 2y ago
How about:
from math import prod # from Python 3.8 onwards
def factorial(n):
return prod(range(n+1))
- oersted 2y agoDefinitely preferable, wasn't aware of it.
- deleted 2y ago[deleted]
- gshubert17 2y agoSince the first element in range() will be 0, should the last line be ...? return prod(1,range(n+1))
- asplake 2y agoOops yes! And unfortunately it still doesn't work for n=0 Edit: prod(range(1, n+1)) does work
- gshubert17 2y agoMy oops too. The extra 1 argument belongs with range, as you have it here.