maths: add O(log n) Fibonacci via matrix exponentiation#14557
maths: add O(log n) Fibonacci via matrix exponentiation#14557AKIB473 wants to merge 2 commits intoTheAlgorithms:masterfrom
Conversation
- Implements fibonacci(n) using fast matrix exponentiation - Time complexity: O(log n), Space complexity: O(log n) - Full docstring with Wikipedia reference - Type hints on all parameters and return values - Doctests for edge cases: fib(0), fib(1), fib(10), fib(20), fib(50) - Raises ValueError for negative input
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
maths/fibonacci_fast.py
Outdated
|
|
||
|
|
||
| def _mat_mul( | ||
| a: list[list[int]], b: list[list[int]] |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: a
Please provide descriptive name for the parameter: b
| return result | ||
|
|
||
|
|
||
| def fibonacci(n: int) -> int: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
4fc96e8 to
66919bd
Compare
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| """ | ||
|
|
||
|
|
||
| def _mat_mul(a: list[list[int]], b: list[list[int]]) -> list[list[int]]: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: a
Please provide descriptive name for the parameter: b
| return result | ||
|
|
||
|
|
||
| def fibonacci(n: int) -> int: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
Describe your change:
Add a new implementation of the Fibonacci sequence using matrix exponentiation, achieving O(log n) time complexity compared to the O(n) iterative approach.
Checklist: