MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _fib

Function _fib

dynamic_programming/fast_fibonacci.py:25–34  ·  view source on GitHub ↗
(n: int)

Source from the content-addressed store, hash-verified

23
24# returns (F(n), F(n-1))
25def _fib(n: int) -> tuple[int, int]:
26 if n == 0: # (F(0), F(1))
27 return (0, 1)
28
29 # F(2n) = F(n)[2F(n+1) - F(n)]
30 # F(2n+1) = F(n+1)^2+F(n)^2
31 a, b = _fib(n // 2)
32 c = a * (b * 2 - a)
33 d = a * a + b * b
34 return (d, c + d) if n % 2 else (c, d)
35
36
37if __name__ == "__main__":

Callers 1

fibonacciFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected