Large integer Multiplication

Source: Internet
Author: User

This is a division of the application, in Java has a large number of classes, with very convenient, but in C + + is not so easy,

An integer is only 2^64 the addition or subtraction of two numbers. We can do it again with an array simulation, but the multiplication of two numbers,

Like giving you two numbers like this:

A= 1234567898765432145673;

b=23456789463784628596936285;

Now asked to find out the c=a*b, the problem is how to implement the algorithm, the array simulation efficiency is too low, an n-bit and M-bit of the number multiplied by a minimum of n*m multiplication,

Before I say how to compute, I'll talk about how to reduce the number of multiplication operations:

Mathematician Gauss once said (A1+b1*i) * (a2+b2*i) Such a two complex number, he can use three times the multiplication operation to calculate the result,

(a1+b1*i) * (a2+b2*i) =a1*b2 + (a1*b2 + b1*a2) *I-B1*B2, take a closer look this is not a 4-time multiplication,

In fact he used a small optimization, assuming not plural (a1+b1) * (A2+B2) = A1*b2 + (a1*b2 + b1*a2) + b1*b2

Then (a1*b2 + b1*a2) = (A1+B1) * (A2+B2)-A1*B2-B1*B2,

So you can (a1+b1*i) * (a2+b2*i) = A1*b2 + ((A1+B1) * (A2+B2)-a1*b2-b1*b2) *i-b1*b2; three times multiplication, got;

What does this have to do with our larger numbers of operations?

Yes, that's true. For two large numbers we're definitely using a divide-and-conquer approach.

Digital a= A1 * 10^ (N/2) + A2;

B=B1 * 10^ (N/2) + B2; (0 if two digits are not the same as long in front of the broken one)

==>> a*b = A1 * B1 * 10^n + (a1* B2 + a2*b1) * 10^ (N/2) + a2*b2

The idea of divided treatment is:

Getans (A, B)

if (a.length = = 1 and B.length = = 1)

return a*b;

Else

Return Getans (A1, B1) *10^n + (Getans (A1, B1) + Getans (A1, B2)) * 10^ (N/2) + Getans (B1, B2);

It's not hard to look at, but I'm going to calculate the time complexity, or the multiplication as the basic operation (with addition as the basic operation to find results and multiplication):

O (1) = 1;

O (n) = 4O (N/2);

Push to the process will not write, but the final figure out the average time complexity is O (n^2), and directly with the array simulation operation of the complexity is the same, spend so much effort to write a 66 code is not 6,

So the optimization, using the Gaussian method to optimize, although the number of additions, but the final result is how it, pushed to a place AH will know:

After a change of posture, there is:

Digital a= A1 * 10^ (N/2) + A2;

B=B1 * 10^ (N/2) + B2; (0 if two digits are not the same as long in front of the broken one)

==>> a*b = A1 * B1 * 10^n + (A*B-A1 * B1 * 10^n-a2*b2) * 10^ (N/2) + a2*b2;

The code will look like this:

Getans (A, B)

if (a.length = = 1 and B.length = = 1)

return a*b;

Else

X <-getans (A1, B1) *10^n;

Y <-getans (A2, B2);

return X + (Getans (A, b)-X-y) *10^ (N/2) + Y;

In order to calculate the complexity of the time becomes:

O (1) = 1;

O (n) = 3*o (N/2);

The last average time complexity becomes O (n * log2,3), which is less than O (n^2),

(The derivation of the average time complexity involves a mathematical transformation, so there is no writing, this is a recursive method of finding a lot of books Mountain also have to speak)

About the specific code, the details are more, and so on after the time to play in the post, welcome to the Big God







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.