Problem
Multiplication of large integers
Idea explanation
For large integer computation, it is generally used in some way to transform, otherwise it will overflow. But Python has no such worries.
Python supports "infinite precision" integers, generally regardless of the problem of integer overflow, and Python int type and arbitrary precision Long Integer class can be seamlessly converted, more than the range of int will be converted to a long type.
For example:
>>> 2899887676637907866*1788778992788348277389943
5187258157415700236034169791337062588991638L
Note: The previous "infinite precision" is enclosed in quotes. In fact there is a limit to the 32-bit machine, the upper limit is: 2^32-1. It's really big enough.
Why is python able to do it? Please be interested to see the source code of Python. This article does not repeat.
In other languages, a "divide-and-conquer" method is often used to solve large integer multiplication problems.
However, here is a very interesting way to compute the multiplication of two integers, which is a demonstration of multiplying large integers.
Multiply two integers: Arabic multiplication. For a detailed description of this multiplication, see: http://ualr.edu/lasmoller/medievalmult.html
Resolution (Python)
#!/usr/bin/env python #coding: Utf-8 #阿拉伯乘法 def arabic_multiplication (num1,num2): Num_lst1 = [Int (i) for i in Str (NUM1) #将int类型的123, convert to [1,2,3] of the list type, each element is an int type num_lst2 = [Int (i) for I-in STR (NUM2)] #两个list中整数两两相乘 Int_martix = [[i* J for-I in Num_lst1] for J-in Num_lst2] #将上述元素为数字的list转化为元素类型是str, is primarily to 9--> ' ' Str_martix = [Map (convert_to_str,in T_martix[i]) for I in Range (len (Int_martix))] #将上述各个list中的两位数字分开: [', ', ', ']-->[0,2,0],[1,9,3] Martix = [[int (Str_martix[i][j][z]) for J-in range (Len (str_martix[i))] for I in range (len (Str_martix)) for z in Range (2)] #计算阿拉伯乘法表的 Left start and Sum_left = Summ_left (Martix) #计算阿拉伯乘法表的底部开始各项和 sum_end = Summ_end (Martix) #将上述两个结果合并后翻转 Sum_left.exten D (sum_end) Sum_left.reverse () #取得各个和的个位的数字 (if rounding is added) result = Take_digit (sum_left) #翻转结果并合并为一个结果字符串数值 result.re Verse () Int_result = "". Join (Result) print "%d*%d="% (num1,num2) print Int_result #将int类型转化为str类型,9--> ' def c ONVERT_TO_STR (num): If Num<10:return "0" +str (num) else:return str (num) #计算阿拉伯乘法表格左侧开始的各项之和 def summ_left (LST): Summ = [] X = [I for I in range (len (LST))] y = [j for j in Range (Len (lst[0]))] SX = [I for i in X if i%2==0] for I in sx:s= 0 j=0 while I>=0 and j<=y[-1]: s = s+ Lst[i][j] If i%2==1:j = j+1 else:j = J i = I-1 Summ.append (s) return Summ #计算阿拉伯乘法表格底部开始的各项之和 def summ_end (LST): summ=[] y = [J-for J-R Ange (Len (lst[0])] ex = Len (LST)-1 for M in range (len (y)): s = 0 I=ex j=m while I>=0 and j<=y[-1 ]: s= s+lst[i][j] If i%2==1:j = j+1 else:j=j i = i-1 summ.append (s) retur N Summ #得到各个元素的个位数, if greater than 10 to the next carry Def take_digit (LST): tmp = 0 digit_list = [] for M in range (len (LST)): lstm = 0 Lstm = lst[m]+tmp if lstm<10:tmp = 0 digit_list.append (str (lstm)) else:tmp = LSTM/1 0 mm = lstm-tmp*10
Digit_list.append (str (mm)) return digit_list if __name__== "__main__": Arabic_multiplication (469,37)