Files
30-seconds-of-code/snippets/factorial.md
Angelos Chalaris 08810dd64e Update some snippets
2019-08-20 10:45:53 +03:00

544 B

title, tags
title tags
factorial math,recursion,beginner

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.

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)
factorial(6) # 720