I am doing an assignment on Karatsuba’s multiplication. I am writing the subtract function. The value of borrow resets to zero after each loop. Why is this?
def subtract(x, y): if len(x) > len(y): y = y.zfill(len(x)) elif len(y) > len(x): x = x.zfill(len(y)) print("Subtract:", x, y) assert x >= y borrow = 0 diff = "" for index in range(len(x) - 1, -1, -1): individual_diff = int(x[index]) - int(y[index]) - borrow if individual_diff < 0: individual_diff += 10 borrow = 1 else: borrow = 0 diff = str(individual_diff % 10) + diff print("result:", diff) return diff
submitted by /u/Abject_Growth9374
[link] [comments]
r/learnpython I am doing an assignment on Karatsuba’s multiplication. I am writing the subtract function. The value of borrow resets to zero after each loop. Why is this? def subtract(x, y): if len(x) > len(y): y = y.zfill(len(x)) elif len(y) > len(x): x = x.zfill(len(y)) print(“Subtract:”, x, y) assert x >= y borrow = 0 diff = “” for index in range(len(x) – 1, -1, -1): individual_diff = int(x[index]) – int(y[index]) – borrow if individual_diff < 0: individual_diff += 10 borrow = 1 else: borrow = 0 diff = str(individual_diff % 10) + diff print(“result:”, diff) return diff submitted by /u/Abject_Growth9374 [link] [comments]
I am doing an assignment on Karatsuba’s multiplication. I am writing the subtract function. The value of borrow resets to zero after each loop. Why is this?
def subtract(x, y): if len(x) > len(y): y = y.zfill(len(x)) elif len(y) > len(x): x = x.zfill(len(y)) print("Subtract:", x, y) assert x >= y borrow = 0 diff = "" for index in range(len(x) - 1, -1, -1): individual_diff = int(x[index]) - int(y[index]) - borrow if individual_diff < 0: individual_diff += 10 borrow = 1 else: borrow = 0 diff = str(individual_diff % 10) + diff print("result:", diff) return diff
submitted by /u/Abject_Growth9374
[link] [comments]