Big Integer Calculation
Big Integer Calculation
Calculation
Addition
Step 1 :
Declare the function
Initialize carry to 0
1. string add(string a,string b)
2. {
3.
string ans;
4.
int tmp,carry=0;
5.
// rest of code
6.
return ans;
7. }
Addition
Step 2 :
Convert char to number from right
side
Now sum=a[i]+b[i]+carry
carry=sum/10 and sum=sum%10
Convert sum to char and add to ans
string
Reverse the string
Addition
1. for(int i=a.size()-1,j=b.size()-1; i>=0||j>=0;
i--,j--)
2. {
3.
if(i<0) tmp=carry+b[j]-'0';
4.
else if(j<0) tmp=carry+a[i]-'0';
5.
else tmp=carry+(a[i]-'0')+(b[j]-'0');
6.
carry=tmp/10;
7.
ans+=(tmp%10)+'0';
8. }
9. if(carry) ans+='1';
10.
reverse(ans.begin(),ans.end());
Lets see the full code
carry=tmp/10;
ans+=(tmp%10)+'0';
}
if(carry) ans+='1';
reverse(ans.begin(),ans.end());
return ans;
Subtraction
Step 1 :
Declare the function
Initialize borrow to 0
1. string subtract(string a,string b)
2. {
3. string ans;
4. int tmp,borrow=0;
5. //Rest of code
6. return ans;
7. }
Subtraction
Step 2 :
Now we have to compare strings to
determine weather a>b or b>a
Compare function returns 1 if a>b , -1 if
b>a and 0 if a=b
If b>a ,we will swap a and b and add a
- sign in the ans string
Lets see our compare function
Subtraction
1. int compare(string a,string b)
2. {
3.
if(a.size()>b.size()) return 1;
4.
if(a.size()<b.size()) return -1;
5.
for(int i=0; i<=a.size()-1; i++)
6.
{
7.
if(a[i]>b[i]) return 1;
8.
if(a[i]<b[i]) return -1;
9.
}
10. return 0;
11.//return(a.compare(b);
12.}
Subtraction
1. string subtract(string a,string b)
2. {
3. int cmp=compare(a,b);
4. If(cmp==0) return "0";
5. if(cmp==-1) swap(a,b);
6.
7.
string ans;
int tmp,borrow=0;
8.
//Rest of code
9. if(cmp==-1) ans="-"+ans;
10. return ans;
11.
}
Subtraction
Step 3 :
Covert char to number from right
side
Now diff=a[i]-b[i]-borrow
If diff<0 then diff=diff+10 and
borrow=1
Convert diff to char and add to ans
string
Subtraction
1. for(int i=a.size()-1,j=b.size()-1; i>=0; i--,j--)
2. {
3.
if(j<0) tmp=a[i] - '0' - borrow;
4.
else tmp=a[i] - b[j] - borrow;
5.
if(tmp<0)
6.
{
7.
tmp+=10;
8.
borrow=1;
9.
}
10.
else borrow=0;
11.
ans+=tmp+'0';
12.
}
13.
reverse(ans.begin(),ans.end());
Subtraction
Step 5 :
12000
11999
Subtraction
1. string remove_leading_zeros(string str)
2. {
3.
int zero=0;
4.
for(int i=0; i<str.size(); i++)
5.
{
6.
if(str[i]=='0') zero++;
7.
else break;
8.
}
9.
if(zero==str.size()) return "0";
10. return str.substr(zero);
11.
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28. }
string ans;
int tmp,borrow=0;
for(int i=a.size()-1,j=b.size()-1; i>=0||j>=0; i--,j--)
{
if(j<0) tmp=a[i]-'0';
else tmp=a[i]-b[j];
if(borrow)
{
tmp--;
borrow=0;
}
if(tmp<0)
{
tmp+=10;
borrow=1;
}
ans+=tmp+'0';
}
reverse(ans.begin(),ans.end());
ans=remove_leading_zeros(ans);
if(cmp==-1) ans="-"+ans;
return ans;
Multiplication
Step 1 :
Declare the function
string multiply(string a,string b)
{
string ans,str;
int tmp,carry;
//Rest of code
return ans;
}
Multiplication
Step 2 :
Multiply all digit of b with a digit of a
from right side each time
sum=carry+(a[i]-'0')*(b[j]-'0')
Now carry=sum/10 and sum=sum%10
Convert sum to char and add to string
Reverse the string
Addition of str with ans each time
Multiplication
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Multiplication
Step 3 :
12345
111
------------12345
123450
1234500
-------------1370295
It is a built-in Class
import java.math.BigInteger ;
Declaration : BigInteger x ;
Initialize to zero : x = BigInteger.ZERO ;
Form string : x = new BigInteger(str) ;
Print : System.out.println(x) ;
Compare : x.compareTo(y) ; //Returns 0 if equal
Addition : ans = x.add(y) ;
Subtraction : ans = x.subtract(y) ;
Multiplication : ans = x.multiply(y) ;
Division : ans = x.divide(y) ;
And many more built-in functions
Java BigInteger
Write a java program that take input of two big integer x , y
and print (x+y) , (x-y) , (x*y) and (x/y) until x=y=0
Sample Input :
123456789
123
0
0
Sample Output :
123456912
123456666
15185185047
1003713
1.
2.
import java.io.*;
import java.math.BigInteger;
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
while (true)
{
String str1 = br.readLine();
String str2 = br.readLine();
13.
14.
15.
16.
17.
18.
19.
20.
}
21.
}
22. }
System.out.println(x.add(y));
System.out.println(x.subtract(y));
System.out.println(x.multiply(y));
System.out.println(x.divide(y));
Related Problems
UVA :
288,324,424,495,623,713,748,10013
,10070,
10083,10106,10183,10220,10302,10
334, 10494,10925,11448
LightOJ : 1024
Happy Coding