A large number multiplication algorithm by calculating quickly thought (i) __ algorithm

Source: Internet
Author: User



A few days ago, it was interesting to find a video:
Http://www.6rooms.com/player.swf?vid=STJd2KkluxggOS7kwW7wuA
The video demonstrates the calculating quickly idea of two-digit multiplication and three-bit multiplication.
You can't see the video, you can see the picture below.

I organized it myself:

12*34=?
Multiplier: 12
By multiplier: 34


First, the multiplier is listed, row I, left, I, Column n times (n is the number of digits of the multiplier)
The second row moves one at a time
(1) (1)
(2) (2)

Write by multiplier, by first column and then in the way
(1,3) (1,4)
(2,3) (2,4)

Multiply () the number in ()
(1,3=3) (1,4=4)
(2,3=6) (2,4=8)

Add, note the rounding
(1,3=3) (1,4=4)
(2,3=6) (2,4=8)
-------------------------
3 10 8
.
-------------------------
4 0 8

12*34=408

Look at the three-digit multiplication again

123*456=?

First step:
(1) (1) (1)
(2) (2) (2)
(3) (3) (3)

Step Two:
(1,4) (1,5) (1,6)
(2,4) (2,5) (2,6)
(3,4) (3,5) (3,6)

Step three:
(1,4= 4) (1,5= 5) (1,6= 6)
         (2,4= 8) (2,5=10) (2,6=12)
                  (3,4 =12) (3,5=15) (3,6=18)

Fourth Step:
(1,4= 4) (1,5= 5) (1,6= 6)
(2,4= 8) (2,5=10) (2,6=12)
(3,4=12) (3,5=15) (3,6=18)
----------------------------------------------
4 13 28 27 18
.        .        . .
----------------------------------------------
5 6 0 8 8

123*456=56088

To analyze how each digit value is calculated, the following is a bit from the Single-digit:
The first bit of the result is the first bit of the multiplier multiplied by the 1 bits of the multiplier, plus the i-1 bit of the multiplier.
With the 2nd bit of the multiplier, together with the 1th bit of the multiplier multiplied by the multiplier of the first bit. This describes
Come a little confused, draw a picture is very clear:
123*456 3rd: From 3rd (1) of the multiplier to 1th (3), press Right-to-left
Multiplied by the multiplier:
1*6+2*5+3*4=28

Then we can add the carry.
Here, a general calculation method has been developed to calculate the results by bit.


Now look at the multiplication of large numbers: first, the multiplier and the multiplier are stored to the array. It's easier.
In C + +, the input content can be treated as a string directly in Io, and can be used in VB.
MID (CStr (k), i,1) to obtain the left-first position of K.
Assuming that the multiplier and the multiplier have been put into the array a () and B () respectively, now define an array C ()
To store the results. The size of C () is the number of digits of the result, which can be computed as follows:
Suppose there are several results to calculate the a*b:
A*b=c
The logarithm of a base of 10 on both sides:
LOG10 (a*b) =log10 (C)
LOG10 (C) is the number of digits, LOG10 (a*b) =log10 (A) +log10 (B), which goes to the formula:
The number of digits in the a*b is an expression (VB):
(log (A) + log (B))/log (10)

After knowing the number of digits, you can define the result array C ().
And then calculate each bit in the way that you want to define a temporary variable to hold the Carry,
Because each number in the array is a 1-digit number, the biggest result is the 9*9=81, the maximum of the rounding variable
And not more than 9.


Multiply the large number, and the speed can be


Factorial speed is relatively slow, more than 500 calculation is laborious, after all, is VB, converted to C + +, and then optimize the calculation should also be

Not to be continued ...

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.