Examine a Python function and refactor it to make it more efficient and optimized.
def foo(n):
    result = 0
    while n > 0:
        result += n
        n -= 1
    return result
def foo(n):
    return n * (n + 1) // 2