601 B
601 B
title, type, tags, cover, dateModified
| title | type | tags | cover | dateModified | ||
|---|---|---|---|---|---|---|
| Factorial | snippet |
|
succulent-11 | 2020-09-15T16:13:06+03:00 |
Calculates the factorial of a number.
- Use recursion.
- If
numis less than or equal to1, return1. - Otherwise, return the product of
numand the factorial ofnum - 1. - Throws an exception if
numis a negative or a floating point number.
def factorial(num):
if not ((num >= 0) and (num % 1 == 0)):
raise Exception("Number can't be floating point or negative.")
return 1 if num == 0 else num * factorial(num - 1)
factorial(6) # 720