usrbinenv python3 this program calculates the nth fibonacci number in ologn it s possible to calculate f1000000 in less than a second return fn fibonaccii for i in range13 0 1 1 2 3 5 8 13 21 34 55 89 144 returns fn fn1 f2n fn2fn1 fn f2n1 fn12fn2 usr bin env python3 this program calculates the nth fibonacci number in o log n it s possible to calculate f 1_000_000 in less than a second return f n fibonacci i for i in range 13 0 1 1 2 3 5 8 13 21 34 55 89 144 returns f n f n 1 f 0 f 1 f 2n f n 2f n 1 f n f 2n 1 f n 1 2 f n 2
from __future__ import annotations

import sys


def fibonacci(n: int) -> int:
    
    if n < 0:
        raise ValueError("Negative arguments are not supported")
    return _fib(n)[0]



def _fib(n: int) -> tuple[int, int]:
    if n == 0:  
        return (0, 1)

    
    
    a, b = _fib(n // 2)
    c = a * (b * 2 - a)
    d = a * a + b * b
    return (d, c + d) if n % 2 else (c, d)


if __name__ == "__main__":
    n = int(sys.argv[1])
    print(f"fibonacci({n}) is {fibonacci(n)}")