大整数乘法 python实现

def recursive_multiply(x, y, n):
   if n==1:
     return x*y
  else:
    a = x/pow(10, n/2)
    b = x-a*pow(10, n/2)
    c = y/pow(10, n/2)
   d = y-c*pow(10, n/2)

   ac = recursive_multiply(a, c, n/2)
   bd = recursive_multiply(b, d, n/2)
   p = recursive_multiply(a+b, c+d, n/2) - ac -bd

   return ac*pow(10, n) + p*pow(10, n/2) + bd

if __name__ == '__main__':
   r = recursive_multiply(1234,5678,4)
   print r

posted @ 2014-06-30 21:59  绿野甘香  阅读(1976)  评论(1编辑  收藏  举报