Introduction
In many situations we need to add/multiply etc integers which have length greater than 100 or 500.for example if we want factorial for 5000 it's huge number, can't save in long, double data type. In such situations we need a data structure that will store such type of numbers.
I wrote this program when I had to use long integers in many problems.
Background
you must have programming background and basic mathematics.
Usage
In this article, I will explain the technique that how to add/multiply etc integers which can't fit in the data types. I will use the basic mathematic for it.
For Additions
First reverse the given two integers for example 1009 and 1.now add them
1009
0001
=====
1010
I am not adding extra '0' but just reverse it and the add them
9001
1
====
0101
Now again reverse them you will get 1010 that's the answer.
For Multiply
The main idea behind the multiplication is given as:
For example
1009
*15
====
15135
First reverse the given integers
9001
51
====
5405
x9001
====
53151
Now again add 5405 and x9001 you will get 53151.Reverse it and that's the final answer
15135
Node that, I am using '0' for 'x' in my code.
here is a piece of code for the multipication
public string s1="123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",s2="222222";
private char[] ary,ary1;
private string result="";
private string[] Final;
<code>
private string LongInt(char[] ary,int mult) </code>
<pre>
{
int num=mult,add=0,mul=0;
string res="";
for(int i=0;i<ary.Length;i++)
{
int v1=(int)(ary[i]);
v1=v1-48;
mul=(v1*num)+add;
string str=mul.ToString();
if(str.Length>1)
{
char[] tmp=str.ToCharArray();
add=(int)(tmp[0]);
add=add-48;
mul=(int)(tmp[1]);
mul=mul-48;
}
else
{
add=0;
}
res +=mul.ToString();
}
if(add!=0)
res +=add.ToString();
res=Reverse(res);
return res;
}
</pre>
<code>
private string Reverse(string st)</code>
<pre>
{
char[] ary=st.ToCharArray();
string ret="";
for(int i=st.Length-1;i>=0;i--)
{
ret +=ary[i];
}
return ret;
}
</pre>
<code>
public void Mul()</code>
<pre>
{
string Org=Reverse(s1);
ary=Org.ToCharArray();
string mult=Reverse(s2);
ary1=mult.ToCharArray();
Final=new string[ary1.Length];
for(int i=0;i<ary1.Length;i++)
{
int mul=(int)ary1[i];
mul -=48;
result=LongInt(ary,mul);
Final[i]=Update(result,i);
}
Addition.addition add=new Addition.addition();
string str="";
for(int i=0;i<Final.Length;i=i+2)
{
add.s1=Final[i];
add.s2=Final[i+1];
str +=add.LongInt();
}
char[] deli=new char[]{'0'};
str=str.TrimStart(deli);
Console.WriteLine(str);
}
</pre>
<code>
private string Update(string str,int val)</code>
<pre>
{
while(val!=0)
{
str =str+"0";
val--;
}
return str;
}
</pre>
now i hope you are able to write your own program for subtraction/division
Comments
if you find and bug/error in the code please reply me,i shall be very thankfull to you.
I did BS in Computer Science from Punjab University,Pakistan.
I have developed lot of Softwares and mostly interested in mathematics,image processing,AI,signal processing,mobile technology,database etc.
Currently working at Confiz Solutions as Sr.Software Engineer.