Skip to content

Home

Factorial

Calculates the factorial of a 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

More like this

Start typing a keyphrase to see matching snippets.