Thursday, March 18, 2010

Multiply

def multiply (x, y):
 if(y < 0 and x > 0):
  return -multiply(x, -y) 
 if(x < 0 and y > 0):
  return -multiply(-x, y)
 if(x < 0 and y < 0):
  return multiply(-x, -y)
 if(y == 0 or x == 0):
  return 0 
 else:
  return x + multiply(x, y - 1)

No comments: