def add(x, y): # O(y) for loop in range(0, y): x = add1(x) return x
def mul(x, y): # O(xy) ret = 0 for loop in range(0, y): ret = add(ret, x) return ret
def sub1(x): # O(x) "Notice sub1(0) is always 0" ret = tmp = 0; for loop in range(0, x): ret = tmp tmp = add1(tmp) return ret
def sub(x, y): # O(xy) "Since sub1(0) is 0, if x < y, sub(x, y) will be 0" for loop in range(0, y): x = sub1(x) return x
def sgn(x): # O(x) "sgn(x) is 0 if x==0 and 1 if x>0" ret = 0 for loop in range(0, x): ret =1 return ret
def div(x, y): # O(x(xy+x)) worst case "Assume a positive number divides by 0 is still that number" quotient = 0 tmp = add1(x) # x++, in case y divides x so the remainder for the loop # in which tmp == y is 0, but it SHOULD be counted a '1' in the quotient for loop in range(0, x): # if x >= y, there should be at most x outer loops # and when tmp < y, sub(tmp, y) is 0 # if x < y, sub(tmp, y) is always 0 tmp = sub(tmp, y) quotient = add(quotient, sgn(tmp)) # every time the reminder still greater than 0, quotient++ return quotient