Optimize this Python code to reduce run time complexity
def function(n):
    result = 0
    for i in range(n):
        for j in range(n):
            result += i * j
    return result
def function(n):
    result = 0
    for i in range(n):
        result += (n * (i * (i+1)) // 2)
    return result