def factorial(n):
    """
    Returns factorial of the integer passed.
    Naive recursive implementation with
    memoization.
    """
    memo = {0: 0, 1: 1}
    # base cases or memoed cases
    try:
        return memo[n]
    except KeyError:
        memo[n] = n * factorial(n - 1)
        return memo[n]