1111
1212
1313class Matrix :
14- def __init__ (self , arg ) :
14+ def __init__ (self , arg : list [ list ] | int ) -> None :
1515 if isinstance (arg , list ): # Initializes a matrix identical to the one provided.
1616 self .t = arg
1717 self .n = len (arg )
1818 else : # Initializes a square matrix of the given size and set values to zero.
1919 self .n = arg
2020 self .t = [[0 for _ in range (self .n )] for _ in range (self .n )]
2121
22- def __mul__ (self , b ) :
22+ def __mul__ (self , b : Matrix ) -> Matrix :
2323 matrix = Matrix (self .n )
2424 for i in range (self .n ):
2525 for j in range (self .n ):
@@ -28,7 +28,7 @@ def __mul__(self, b):
2828 return matrix
2929
3030
31- def modular_exponentiation (a , b ) :
31+ def modular_exponentiation (a : Matrix , b : int ) -> Matrix :
3232 matrix = Matrix ([[1 , 0 ], [0 , 1 ]])
3333 while b > 0 :
3434 if b & 1 :
@@ -38,7 +38,7 @@ def modular_exponentiation(a, b):
3838 return matrix
3939
4040
41- def fibonacci_with_matrix_exponentiation (n , f1 , f2 ) :
41+ def fibonacci_with_matrix_exponentiation (n : int , f1 : int , f2 : int ) -> int :
4242 """
4343 Returns the nth number of the Fibonacci sequence that
4444 starts with f1 and f2
@@ -64,7 +64,7 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
6464 return f2 * matrix .t [0 ][0 ] + f1 * matrix .t [0 ][1 ]
6565
6666
67- def simple_fibonacci (n , f1 , f2 ) :
67+ def simple_fibonacci (n : int , f1 : int , f2 : int ) -> int :
6868 """
6969 Returns the nth number of the Fibonacci sequence that
7070 starts with f1 and f2
@@ -95,7 +95,7 @@ def simple_fibonacci(n, f1, f2):
9595 return f2
9696
9797
98- def matrix_exponentiation_time ():
98+ def matrix_exponentiation_time () -> float :
9999 setup = """
100100from random import randint
101101from __main__ import fibonacci_with_matrix_exponentiation
@@ -106,7 +106,7 @@ def matrix_exponentiation_time():
106106 return exec_time
107107
108108
109- def simple_fibonacci_time ():
109+ def simple_fibonacci_time () -> float :
110110 setup = """
111111from random import randint
112112from __main__ import simple_fibonacci
@@ -119,7 +119,7 @@ def simple_fibonacci_time():
119119 return exec_time
120120
121121
122- def main ():
122+ def main () -> None :
123123 matrix_exponentiation_time ()
124124 simple_fibonacci_time ()
125125
0 commit comments