File tree 1 file changed +6
-4
lines changed
1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change 4
4
Iterative Karatsuba multiplication algorithm
5
5
"""
6
6
7
+ from collections import deque
8
+
7
9
number_base_bit_shift = 16
8
10
number_base = 1 << number_base_bit_shift # 65536
9
11
@@ -43,13 +45,13 @@ def karatsuba_split_inputs(multiplicand, multiplier):
43
45
def karatsuba_multiply_iterative (multiplicand , multiplier ):
44
46
# The node stack holds the input arguments to the Karatsuba multiplication function along with the branch of each
45
47
# node in the tree with respect to its parent
46
- node_stack = [[multiplicand , multiplier , 0 ]] # Assign root node with left branch 0
48
+ node_stack = deque ( [[multiplicand , multiplier , 0 ]]) # Assign root node with left branch 0
47
49
48
50
# These stacks hold the results of lower depth calculations and maintain information about which higher order
49
51
# calculations they belong to
50
- branch_path = []
51
- m_stack = []
52
- z_stack = [[], [], [] ]
52
+ branch_path = deque ()
53
+ m_stack = deque ()
54
+ z_stack = [deque (), deque (), deque () ]
53
55
leaf_count = 0
54
56
55
57
# Perform the depth first tree traversal and calculate the results - since the recursive Karatsuba multiplication
You can’t perform that action at this time.
0 commit comments