"Algorithm design and Analysis basis" large integer multiplication

Source: Internet
Author: User
Tags mul

#include <iostream> #include <string> #include <time.h> #include <stdlib.h> #include < Sstream>using namespace Std;class bigdecimal{private:int max (int a,int b) {//Get maximum value in two numbers return a^ ((a^b) &-(A&LT;B) );} public:string N; BigDecimal () {n= "0";} void SetString (string n) {this->n=n;} BigDecimal (string n) {this->n=n;} BigDecimal operator + (BigDecimal b) {string s,l;//s is Addend, L is addend int temp=0,i,j;//temp record carry, I is used to traverse the s,j to determine which number is longer and longer gives S, Short to Lif (N.length () >=b.n.length ()) {s=n;l=b.n;i=s.length () -1;j=b.n.length ()-1;} Else{s=b.n;l=n;i=s.length () -1;j=n.length ()-1;} Start the addition operation while (j>=0) {s[i]=s[i]+l[j]+temp-48;//Two bits add, plus carry, minus 48 (' 0 ' ascii) if (s[i]> ' 9 ') {//= Two if the add is greater than 10, minus 10 , and then into position 1s[i]-=10;temp=1;} Else{temp=0;} i--;j--;} If l goes through, the carry is still 1, it consumes the carry until it is 0while (i>=0 && temp==1) {s[i]++;if (s[i]>=58) {s[i]-=10;i--;} Else{temp=0;break;}} If there is a carry after reaching the highest bit, the number is preceded by a ' 1 ' if (temp) {s= ' 1 ' +s;} return BigDecimal (s);} BigDecimal operator-(BigDecimal b) {string s,e;//s is meiosis, E is meiosis/** * due to (a1+a0) * (b1+b0)-(A1*B1+A0*B0) = A1 * B0 + a0 * B1 * so the subtraction operation is not negative */s=n; E=b.n;int i=s.length () -1,j=b.n.length () -1,temp=0;//begins the subtraction while (j>=0) {s[i]=s[i]+48-e[j]-temp;//Two-bit subtraction, minus borrow, Plus 48if (s[i]< ' 0 ') {//If not enough minus, add 10, borrow position 1s[i]+=10;temp=1;} Else{temp=0;} i--;j--;} If borrow is still 1, consume borrow until 0while (i>=0 && temp) {s[i]--;if (s[i]< ' 0 ') {s[i]+=10;temp=1;} Else{temp=0;}} i=0;//If the number begins with 0, remove the while (S.length () > 1) {if (s[i]== ' 0 ') {s.erase (0,1);} Else{break;}} Return BigDecimal (S);} BigDecimal Mul (string a,string b) {//int is multiplied by int, int num = atoi (A.C_STR ()) * Atoi (B.C_STR ()), if (num==0) return BigDecimal (); String S;stringstream Ss;ss<<num;ss>>s;return BigDecimal (s);} BigDecimal mul (String a,int N) {if (a== "0") {return BigDecimal ();} Char *k=new char[n+1];memset (k, ' 0 ', n*sizeof (char)); k[n]= ' + '; string Temp;a.append (k);d Elete[]k;return BigDecimal (A );} void Sub (int center,bigdecimal *s1,bigdecimal *s0) {if (N.length () <=center) {//If the large integer does not have enough length to be divided into two halves/** * that a1=0 a0=n * * S0->n=n.substr (0,n.length ());} else{/** * Otherwise it will be divided intoTwo halves */s0->n=n.substr (N.length ()-center,center); S1->n=n.substr (0,n.length ()-center);}} BigDecimal operator * (BigDecimal b) {//If the array length is less than or equal to 4, call the original multiplication if (N.length () <=4 && b.n.length () <=4) {return Mul (N,B.N);} BigDecimal Temp, A1, A0, B1, B0, C2, C1, C0;int Maxbit=max (N.length (), b.n.length ()); Sub (MAXBIT/2, &AMP;A1, &amp ; a0); B.sub (MAXBIT/2, &AMP;B1, &b0); c2 = a1 * B1;c0 = a0 * b0;c1 = (A1 + A0) * (B1 + b0)-(C2 + C0); return Mul (C2.N , maxbit/2*2) + mul (C1.N, MAXBIT/2) + C0;}};    int main () {clock_t start, finish; Double Totaltime;srand ((unsigned int) time (0)), int bit=10;//here to modify the number of bits of two large integers int pos=1;int i,j;string s,m;cout<<bit << ":" <<endl;do{j=rand ()%10+48;} while (j== ' 0 '), s+= (char) j;for (i=0;i<bit-1;i++) {J=rand ()%10+48;s+= (char) J;} Do{j=rand ()%10+48;} while (j== ' 0 '), m+= (char) j;for (i=0;i<bit-1;i++) {J=rand ()%10+48;m+= (char) J;} cout<<s<< "*" <<m<< "=" <<endl; BigDecimal A (s), B (M), C;start=clock (); C=a * B;finish=clock (); Cout<<c.n<<endl;totaltime= (double) (Finish-start)/clocks_per_sec;cout<< "This program runs for" << totaltime<< "Seconds!   "<<endl; return 0;}


If you need to do it faster, you need to write a primitive multiplication yourself and then

BigDecimal operator * (BigDecimal b) {
If the array length is less than or equal to 4, the original multiplication is called directly
if (N.length () <=4 && b.n.length () <=4) {
Return Mul (N,B.N);
}
BigDecimal Temp, A1, A0, B1, B0, C2, C1, C0;
int Maxbit=max (N.length (), b.n.length ());
Sub (MAXBIT/2, &AMP;A1, &a0);
B.sub (MAXBIT/2, &AMP;B1, &b0);
c2 = A1 * B1;
C0 = a0 * B0;
C1 = (A1 + A0) * (B1 + b0)-(C2 + C0);
Return Mul (C2.N, maxbit/2*2) + mul (C1.N, MAXBIT/2) + C0;
}

Modified in

if (N.length () <=4 && b.n.length () <=4) {
Return Mul (N,B.N);
}

4 is 600, then change Mul (N,B.N) to your original multiplication function mul (string,string)

Explanation and analysis of large integer algorithm ppt:http://download.csdn.net/detail/u013580497/8888515

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Algorithm design and Analysis basis" 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.