Multiply by large number

Source: Internet
Author: User

Http://www.cnblogs.com/jason-yang/archive/2012/04/26/2472755.html

Today on the Internet to see a large number multiplication problem, the topic is this: Enter two integers, requires the output of the two number of products. The number you enter may exceed the storage scope of your computer's shaping data.

Analysis:

Since numbers cannot be stored with an shaping variable, it is natural to think of strings representing a string of numbers. Then, according to the multiplication rule, multiply each bit of a multiplier by another multiplier and then add all the intermediate results in the correct position to get the final result. It can be analyzed that if the multiplier is a and b,a the number of bits to M,b is N, then the product result is m+n-1 bit (highest bit no carry) or m+n bit (highest bit has carry). Therefore, you can allocate a m+n to store the final result. In order to save space, all intermediate results are added directly to the m+n of the secondary storage. Finally, in order to better conform to our multiplication logic, we can talk about digital reverse storage, so that the low number of the array is in the lower position of the index, the accumulation of the position of the subscript is easier to determine.

Here is my solution.

The first is the function of the inverse of the array:

void reverseorder (charintint  q) {    char  temp;      while (P < q)    {        = str[p];         = Str[q];         = temp;         ++;         --;    }}

Char* Multilargenum (CharAChar*B) {    intm =strlen (A); intn =strlen (B); Char* result =New Char[m+n+1]; memset (Result,'0', m+N); Result[m+n] =' /'; Reverseorder (A,0, M-1); Reverseorder (B,0, N-1); intMultiflag;//Product Rounding    intAddflag;//Addition Rounding     for(intI=0; I <= N-1; i++)//every bit of B{Multiflag=0; Addflag=0;  for(intj=0; J <= M1; J + +)//every bit of a        {            //' 0 '-= 0            intTemp1 = (A[j]- -) * (B[i]- -) +Multiflag; Multiflag= TEMP1/Ten; Temp1= Temp1%Ten; intTemp2 = (Result[i+j]- -) + Temp1 +Addflag; Addflag= TEMP2/Ten; Result[i+J] = temp2%Ten+ -; } result[i+ m] + = Multiflag +Addflag; } reverseorder (Result,0, m+n-1);//Back in reverse    returnresult;}
intMain () {CharA[] ="962346239843253528686293234124"; CharB[] ="93459382645998213649236498"; Char*res =Multilargenum (A, B); if(res[0] != -) printf ("%c", res[0]); printf ("%s", res+1); Delete[] Res; return 0;}

Time Complexity Analysis:

The time of 3 reverse operations is O (n), O (M), O (m+n), and the time complexity of the double loop is O (MN), the total time complexity is O (MN + (m+n)), usually M+n << mn, so it can be approximated as O (MN). Moreover, the reverse operation is only easier to think about and can be removed completely.

Spatial complexity of O (m+n)

Multiply by large number

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.