11"""Matrix Exponentiation"""
22
3+ from __future__ import annotations
4+
35import timeit
46
57"""
1113
1214
1315class Matrix :
14- def __init__ (self , arg ) :
16+ def __init__ (self , arg : list [ list [ int ]] | int ) -> None :
1517 if isinstance (arg , list ): # Initializes a matrix identical to the one provided.
1618 self .t = arg
1719 self .n = len (arg )
1820 else : # Initializes a square matrix of the given size and set values to zero.
1921 self .n = arg
2022 self .t = [[0 for _ in range (self .n )] for _ in range (self .n )]
2123
22- def __mul__ (self , b ) :
24+ def __mul__ (self , b : Matrix ) -> Matrix :
2325 matrix = Matrix (self .n )
2426 for i in range (self .n ):
2527 for j in range (self .n ):
@@ -28,7 +30,7 @@ def __mul__(self, b):
2830 return matrix
2931
3032
31- def modular_exponentiation (a , b ) :
33+ def modular_exponentiation (a : Matrix , b : int ) -> Matrix :
3234 matrix = Matrix ([[1 , 0 ], [0 , 1 ]])
3335 while b > 0 :
3436 if b & 1 :
@@ -38,7 +40,7 @@ def modular_exponentiation(a, b):
3840 return matrix
3941
4042
41- def fibonacci_with_matrix_exponentiation (n , f1 , f2 ) :
43+ def fibonacci_with_matrix_exponentiation (n : int , f1 : int , f2 : int ) -> int :
4244 """
4345 Returns the nth number of the Fibonacci sequence that
4446 starts with f1 and f2
@@ -64,7 +66,7 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
6466 return f2 * matrix .t [0 ][0 ] + f1 * matrix .t [0 ][1 ]
6567
6668
67- def simple_fibonacci (n , f1 , f2 ) :
69+ def simple_fibonacci (n : int , f1 : int , f2 : int ) -> int :
6870 """
6971 Returns the nth number of the Fibonacci sequence that
7072 starts with f1 and f2
@@ -95,7 +97,7 @@ def simple_fibonacci(n, f1, f2):
9597 return f2
9698
9799
98- def matrix_exponentiation_time ():
100+ def matrix_exponentiation_time () -> float :
99101 setup = """
100102from random import randint
101103from __main__ import fibonacci_with_matrix_exponentiation
@@ -106,7 +108,7 @@ def matrix_exponentiation_time():
106108 return exec_time
107109
108110
109- def simple_fibonacci_time ():
111+ def simple_fibonacci_time () -> float :
110112 setup = """
111113from random import randint
112114from __main__ import simple_fibonacci
@@ -119,7 +121,7 @@ def simple_fibonacci_time():
119121 return exec_time
120122
121123
122- def main ():
124+ def main () -> None :
123125 matrix_exponentiation_time ()
124126 simple_fibonacci_time ()
125127
0 commit comments