본문 바로가기

Programming Language/Python3.6

[Python 파이썬] 피보나치 수열 구현하기 (공간 최적화)

def fib_optimized(n):
    current = 1              # current는 1로 초기화, n = 1이면 반복 필요 없음
    previous = 0

    # 반복문 구현
    for i in range(1, n):
        current, previous = current + previous, current

    # n번재 피보나치 수를 리턴
    return current


# 테스트 코드
print(fib_optimized(100)) # 실행결과: 354224848179261915075

반응형