Automatically analyze and optimize the given Python code snippet.
def factorial(n):
 if n == 1:
 return 1
 else:
 return n*factorial(n-1)
def factorial(n):
 return 1 if n in (0, 1) else n * factorial(n - 1)