오경석의 개발노트

Python 알고리즘_1부터 n까지 연속한 숫자의 제곱의 합 본문

알고리즘/Python 알고리즘

Python 알고리즘_1부터 n까지 연속한 숫자의 제곱의 합

OHSAYU 2022. 11. 13. 15:51
# algorithm_1, 계산복잡도 : O(n)
def squared1(n):
    result = 0
    for i in range(1, n + 1):
        result += i * i
    return result
# algorithm_2, 계산복잡도 : O(1)
def squared_2(n):
    return n * (n + 1) * (2 * n + 1) // 6
Comments