File tree Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ def _power (m : list [int ], n : int ) -> list [int ]:
2
+ result = [1 , 0 , 0 , 1 ]
3
+ while n > 0 :
4
+ if n % 2 == 1 :
5
+ result = _multiply (result , m )
6
+
7
+ m = _multiply (m , m )
8
+ n //= 2
9
+
10
+ return result
11
+
12
+
13
+ def _multiply (a : list [int ], b : list [int ]) -> list [int ]:
14
+ return [
15
+ a [0 ] * b [0 ] + a [1 ] * b [2 ],
16
+ a [0 ] * b [1 ] + a [1 ] * b [3 ],
17
+ a [2 ] * b [0 ] + a [3 ] * b [2 ],
18
+ a [2 ] * b [1 ] + a [3 ] * b [3 ],
19
+ ]
20
+
21
+
22
+ def python_nth_fibonacci_using_matrix_exponentiation (n : int ) -> int :
23
+ if n == 0 :
24
+ return 0
25
+ else :
26
+ base = [1 , 1 , 1 , 0 ]
27
+ result = _power (base , n - 1 )
28
+ return result [0 ]
Original file line number Diff line number Diff line change
1
+ import test_rust_in_python
2
+ from fibonacci_matrix_exponentiation import python_nth_fibonacci_using_matrix_exponentiation
3
+
4
+
5
+ RUST_FUNCTIONS = test_rust_in_python
6
+
7
+ rust_fibonacci_reuslt = RUST_FUNCTIONS .rust_nth_fibonacci_using_matrix_exponentiation (93 ) # type: ignore
8
+
9
+ print (f"Result from Rust: { rust_fibonacci_reuslt } " )
10
+
11
+
12
+ python_fibonacci_reuslt = python_nth_fibonacci_using_matrix_exponentiation (93 )
13
+ print (f"Result from Python: { python_fibonacci_reuslt } " )
You can’t perform that action at this time.
0 commit comments