Large integer Multiplication

Source: Internet
Author: User

problem

For the multiplication of two large integers, for example, the number of two digits is 1024x768, and the C language cannot represent such a large number. But we can use the Division method to find their product.

Solve

Without losing its generality, two numbers a and B are n bits, and n is the power of 2. If this condition is not met, you can make them satisfy by filling the 0 operation.

A:

A1 A2

B:

B1 B2

Then a*b can be written as:

A*b = (A1*10^N/2 + A2) * (B1*10^N/2 + b2)
= A1*b1*10^n + (A1*B2+A2*B1) *10^N/2 + a2*b2

If you only switch to this point, then the time complexity of the algorithm is still very high

The recursive relationship is: T (n) = 4T (N/2) + O (n), because after decomposition, there are 4 different multiplication operations

After calculation, the time complexity is O (n^2)

We can also continue to optimize. A1*b1*10^n + (A1*B2+A2*B1) *10^N/2 + a2*b2, in this equation, A1*B2+A2*B1 can be expressed using A1*B1 and A2*B2.

A1*B2+A2*B1 = (A1-A2) * (B2-B1) + A1*B1 + a2*b2

In this way, a*b can be expressed as:

A1*b1*10^n + ((A1-A2) * (B2-B1) + a1*b1 + a2*b2) *10^N/2 + a2*b2

There are 3 different multiplication in this one. The time complexity of the add and subtract operation is O (n)

The recursive relationship is: T (n) = 3T (N/2) + O (n)

Time complexity: O (n^log3) = O (n^1.59)

Large integer 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.