Python implements large integer multiplication---lattice multiplication

Source: Internet
Author: User
Tags pprint

When I used to do ACM, many people through the BigInteger to achieve large number multiplication, let me remember the 2012 Liaoning province in Dalian University, the first water problem is the big integer multiplication, then not java. The implementation of the large-number multiplication is based on the lattice multiplication of India, which uses this method to calculate the number of M-digits multiplied by the n-digits only need to create an array of m+n bits to save the result.

Today we will use Python to simulate the operation of the lattice algorithm, Python to write the algorithm is very simple.

Here are the pictures and detailed steps you've climbed down from Wikipedia.

The first step: Draw a diagonal lattice, the first number (58) is written at the top of the lattice, the second number (213) is written on the right side of the lattice, the lattice slash below the write turned out jide single digit, the lattice slash above the 10-digit number of the product.

Step two: Multiply each lattice top number by the number on the right side of the same lattice, write the product one by one into the lattice, and then add the number to the bottom by a slash, adding the resulting sum to the grid or to the left:

Step three: From the left side of the lattice to the top, then the bottom of the grid from left to right, read Chuyang product: 12354

So the x 213=12354

the idea of my algorithm in the picture below, please the prawns correct ah ..... Algorithm note write it tomorrow, it's slightly late today ... Unexpectedly a small algorithm wrote for two hours .....


#-*-Coding:utf-8-*-import pprintimport sysdef multiplication (num1,num2): Num_list1 = [Int (i) for i in Str (NUM1)] NUM_LIST2 = [Int (i) for i in Str (num2)] Int_martix = [[I*j for I in Num_list1] for J in Num_list2] #pprint. Pprint (in  T_martix) Result_martix = Convert (Int_martix) #pprint. Pprint (Result_martix) X_len = Len (Result_martix)-1 Y_len = Len (result_martix[0])-1 X=x_len y=y_len #print x, y result = str (result_martix[x][y]) #print result WH Ile (x! = 0 and Y! = 0): jinwei_past = 0 for i in range (y): m = x n = y n =                N-i-1 #print result_martix[m][n] temp = 0 while (m>=0 and N<=y_len): Temp = temp + result_martix[m][n] m = m-1 n = n + 1 #print temp JINW Ei_now = TEMP/10 num = Int (temp%10+jinwei_past) If Num>=10:jinwei_now = Jinwei_no W + 1 result= str (num%10) + result jinwei_past = Jinwei_now #print result y = 0 for I in range (x+1                ): M = x n = 0 m = M-1-i temp = 0 while (m>=0 and N<=y_len): Temp = temp + result_martix[m][n] m = m-1 n = n + 1 Jinwei_now =            TEMP/10 num = Int (temp%10+jinwei_past) If Num>=10:jinwei_now = Jinwei_now + 1        result = STR (NUM%10) + result jinwei_past = Jinwei_now x = 0 for I in range (len (result)): If result[i]! = ' 0 ': result = Result[i:] Print result break def convert (ma Rtix): Return_martix = [[(J[I]%10+J[I+1]/10) for I in Range (0,len (j)-1)] "for J in Martix] i = 0 for J in Mart IX: #print i,j Return_martix[i].insert (0,J[0]/10) Return_martix[i].append (J[len (j) -1]%10) i = i + 1 if i = = Len(Martix): Break return Return_martix def main (): print "Please enter two integers:" num1,num2= map (Int,sys.stdin. ReadLine (). Split ()) multiplication (num1,num2) main ()
From: http://blog.csdn.net/djd1234567

Python implements large integer multiplication---lattice multiplication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.