Created
February 27, 2025 18:29
-
-
Save msp729/f009c7cb4777d00ea3c91dffb5c252d5 to your computer and use it in GitHub Desktop.
Python compact matrix Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FLM: | |
| iden: int | |
| step: int | |
| def sum(self): | |
| return self.iden + self.step | |
| def __init__(self, iden = 0, step = 1): | |
| self.iden = iden | |
| self.step = step | |
| def __mul__(self, other): | |
| step2 = self.step * other.step | |
| step0 = self.iden * other.iden | |
| step1 = self.sum() * other.sum() - step0 - step2 | |
| return FLM(step0 + step2, step1 + step2) | |
| def __str__(self): | |
| return f"FLM[{self.sum()} {self.step}\n {self.step} {self.iden}]" | |
| def __pow__(self, idx): | |
| ret = FLM(1, 0) | |
| base = self | |
| while idx: | |
| if idx % 2: | |
| ret *= base | |
| idx //= 2 | |
| base *= base | |
| return ret | |
| def fib(n): | |
| return (FLM() ** n).step |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment