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

Class Fibonacci

dynamic_programming/fibonacci.py:7–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7class Fibonacci:
8 def __init__(self) -> None:
9 self.sequence = [0, 1]
10
11 def get(self, index: int) -> list:
12 """
13 Get the Fibonacci number of `index`. If the number does not exist,
14 calculate all missing numbers leading up to the number of `index`.
15
16 >>> Fibonacci().get(10)
17 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
18 >>> Fibonacci().get(5)
19 [0, 1, 1, 2, 3]
20 """
21 if (difference := index - (len(self.sequence) - 2)) >= 1:
22 for _ in range(difference):
23 self.sequence.append(self.sequence[-1] + self.sequence[-2])
24 return self.sequence[:index]
25
26
27def main() -> None:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected