factorial of a number using memoization factorial7 5040 factorial1 traceback most recent call last valueerror number should not be negative factoriali for i in range10 1 1 2 6 24 120 720 5040 40320 362880 factorial of a number using memoization factorial 7 5040 factorial 1 traceback most recent call last valueerror number should not be negative factorial i for i in range 10 1 1 2 6 24 120 720 5040 40320 362880
from functools import lru_cache


@lru_cache
def factorial(num: int) -> int:
    
    if num < 0:
        raise ValueError("Number should not be negative.")

    return 1 if num in (0, 1) else num * factorial(num - 1)


if __name__ == "__main__":
    import doctest

    doctest.testmod()