factorial

This commit is contained in:
Rohit Tanwar
2018-01-27 10:59:56 +05:30
parent 2352653082
commit 2bcb100b85

15
snippets/factorial.md Normal file
View File

@ -0,0 +1,15 @@
### factorial
Calculates the factorial of a number.
Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number.
```python
def factorial(num):
if not ((num >= 0) & (num % 1 == 0)):
raise Exception(
f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1)
```