Sei sulla pagina 1di 3


Multiplication of Large Numbers. - Krishna Bharadwaj

Krishna Bharadwaj
Geek, programmer, foss enthusiast, loves Python, Ex-Microsoft Student Partner, Ex-National Instruments. You can Subscribe to my blog, Follow me on twitter Home Blog Links About Facebook Twitter Mail LinkedIn GitHub TopCoder Spoj Stackoverflow Feeds

Multiplication of Large Numbers.

I follow discussions on the Algorithms community on orkut. Came across one question which asked us to implement very big integers in C/C++. We can calculate 12! using integer data type, 20! using long long. Java provides a data type BigInteger which handle it, Python can handle REALLY LARGE VALUES. In C/C++ we can implement this with the help of an integer array whe digit is represented as an element of the array. I have used a class for this purpose. Here's my implementation of a large number in C++.. #include<iostream> #include<string> #include<sstream> #define SIZE 700 using namespace std; class large{ int no[SIZE]; /* array is used to store the large number. Each digit of a the number is stored as an element.. 123 is stored as no[3]={1,2,3} */ public: // overload the + operator large operator +(large& x){ int carry=0; large ret; for(int i=SIZE-1;i>=0;i--){ carry += no[i][i]; if(carry < 10){[i]=carry; carry=0; } else{[i]=carry%10; carry=carry/10; } } return ret; } large operator *(large& x){ // overload the * operator /* 34 x 46 ------204 // these values are stored in the 136 // two dimensional array mat[][]; ------1564 // this the value stored in "large ret" */ large ret; int carry=0; int mat[2*SIZE+1][2*SIZE]={0}; for(int i=SIZE-1;i>=0;i--){ for(int j=SIZE-1;j>=0;j--){ carry += no[i]*[j]; if(carry < 10){ mat[i][j-(SIZE-1-i)]=carry; carry=0; } else{



Multiplication of Large Numbers. - Krishna Bharadwaj

mat[i][j-(SIZE-1-i)]=carry%10; carry=carry/10; } } } for(int i=1;i<SIZE+1;i++){ for(int j=SIZE-1;j>=0;j--){ carry += mat[i][j]+mat[i-1][j]; if(carry < 10){ mat[i][j]=carry; carry=0; } else{ mat[i][j]=carry%10; carry=carry/10; } } } for(int i=0;i<SIZE;i++)[i]=mat[SIZE][i]; return ret; } large (){ for(int i=0;i<SIZE;i++) no[i]=0; } large (string _no){ for(int i=0;i<SIZE;i++) no[i]=0; int index=SIZE-1; for(int i=_no.length()-1;i>=0;i--,index--){ no[index]=_no[i]-'0'; } } //print the large number void print(){ int start=0; // find the first non zero digit. store the index in start. for(int i=0;i<SIZE;i++) if(no[i]!=0){ start=i; break; } // print the number starting from start till the end of array. for(int i=start;i<SIZE;i++) cout<<no[i]; cout<<endl; return; } }; int main(){ large fact("1"); for(int i=1;i<=300;i++){ stringstream io; // the input must be taken in the form of a string; string number; io << i; io >> number; large ite(number); fact = fact * ite; } fact.print(); return 0; }
This Gist brought to you by GitHub. LargeInteger.cpp

GNU/Linux Kernel Tutorial!

Tweet 0

Unforgettable Day!



Multiplication of Large Numbers. - Krishna Bharadwaj

Add New Comment

Showing 2 comments
Avi Dullu

Sort by popular

Heard of Discrete Fourier Transform?

1 month ago Krishna Bharadwaj

Hi Avi, I have heard of DFT, but never thought about it when I wrote this 3 years back. Thanks
1 month ago in reply to Avi Dullu

M Subscribe by email S RSS

blog comments powered by DISQUS