[Algorithm]: Divide and conquer-the multiplication of large integers

Source: Internet
Author: User
Tags pow

Tag: Size description tin GitHub Ace alt return TPS overflow

#problem
Multiply large integers

#Idea Description



python support * * "infinite precision" integer, * * Generally do not consider the problem of integer overflow, and python A long integer class of type int and arbitrary precision can be transformed seamlessly, and cases that exceed the int range will be converted to a long type.

For example:

>>> 2899887676637907866*1788778992788348277389943

5187258157415700236034169791337062588991638L

note: The preceding" infinite precision "is quoted. In fact there is a limit, for 32-bit machines, the upper limit is: 2^32-1. It's really big enough.

Why would Python do that? Please be interested in inquiring about the source code of Python. This article does not repeat.




< Span style= "COLOR: #cf6a4c" > re-simplification: xy=ac2^n+[(A-B) (d-c) +ac+bd]2^n/2+bd
This algorithm requires that x, y must be the same number of bits, and that the number of bits is 2 n
So: 1. Save with array, not meet the conditions in front of 0
2. Multiply the short fill 0 until the x, y digits of the same
3. Recursion
/span>
Import mathimport copyclass Big_int (list): Def __init__ (self, num): Self.neg = 1 for i in STR (num): if i = = '-': Self.neg =-1 Continue self.append (int (i)) m =math.log ( Len (self), 2) if M!=int (m): M=int (m) +1 while Len (self)! = Pow (2, m): Self.inse RT (0, 0) @classmethod def minusa (cls,l1,l2): l1 = int (str (L1). replace (' [', ') '. Replace ('] ', '). Replace (', ', ') . replace (",")) L2 = Int (str (L2). replace (' [', '). Replace (', ', '). Replace (', ', '). Replace (', ')) retur            n l1-l2 def __aspect (SELF,L1,L2): L1=copy.copy (L1) l2=copy.copy (L2) n = len (L1)/2 if n==0:        Return l1.neg*l2.neg* (l1[0]*l2[0]) A = l1[:n] B = l1[n:] C = l2[:n] D = l2[n:]        if N = = 1:return l1.neg*l2.neg* (a[0]*c[0]*100 + ((a[0]-b[0)) * (D[0]-c[0]) +a[0]*c[0]+b[0]*d[0])            ElseA_,b_,c_,d_= "," "," "," for I in A:a_ + = str (i) for i in b:b_ + = str (i) for i in c:c_ + = str (i) for i in D:d_ + = str (i) A = b            Ig_int (a_) B = Big_int (b_) C = Big_int (c_) D = Big_int (d_) AC = a.__mul__ (c)        BD = b.__mul__ (D) a_b = Big_int (Big_int.minusa (b)) D_c = Big_int (Big_int.minusa (d,c))        Return l1.neg*l2.neg* (Ac*pow (10,2*n) + (a_b.__mul__ (d_c) +ac+bd) *pow (10,n) +BD) def __fillzero_mul (self, Other):            If Len (self) >len (other): while (Len (other) <len (self): Other.insert (0,0) Else: While, Len (self), <len (Other): Self.insert (0,0) return Self.__aspect (self,other) def __ Mul__ (self, Other): if Type (other) is not big_int:raise return Self.__fillzero_mul (Other)
Reference: http://blog.csdn.net/jeffleo/article/details/53446095
Https://github.com/qiwsir/algorithm

[Algorithm]: Divide and conquer-the multiplication of large integers

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.