0% found this document useful (0 votes)
66 views

Big Integer Calculation

This document discusses methods for performing arithmetic operations on big integers using C++ and Java. It provides code to add, subtract, multiply and divide two big integers represented as strings in C++ using custom functions. For Java, it demonstrates how to use the built-in BigInteger class to perform the same operations in a more straightforward way by taking input, initializing BigIntegers from strings, and calling member functions to add, subtract, multiply and divide until both numbers equal zero. Finally, it lists some programming challenges involving big integer arithmetic from sources like UVA and LightOJ.

Uploaded by

Zulqarnayn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Big Integer Calculation

This document discusses methods for performing arithmetic operations on big integers using C++ and Java. It provides code to add, subtract, multiply and divide two big integers represented as strings in C++ using custom functions. For Java, it demonstrates how to use the built-in BigInteger class to perform the same operations in a more straightforward way by taking input, initializing BigIntegers from strings, and calling member functions to add, subtract, multiply and divide until both numbers equal zero. Finally, it lists some programming challenges involving big integer arithmetic from sources like UVA and LightOJ.

Uploaded by

Zulqarnayn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Big Integer

Calculation

CUET COMPUTER CLU


Prepared by Dipta Das

Using C++ String

About C++ String

It is a built-in Class not data type


Header File : # include <string>
Declaration : string str;
Initialize : str = I am a string;
Input word : cin>>str;
Input line : getline(cin , str);
Print : cout<<str;
Access elements : str[i]
Add element : str += c ;
Join : str = Computer + Club ;
Length : int ln = str.size();
Reverse : reverse( str.begin() , str().end() ) ;
Call by value unlike char array

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

1. string add(string a,string b)


2. {
3.
string ans;
4.
int tmp,carry=0;
5.
for(int i=a.size()-1,j=b.size()-1; i>=0||j>=0; i--,j--)
6.
{
7.
if(i<0) tmp=carry+b[j]-'0';
8.
else if(j<0) tmp=carry+a[i]-'0';
9.
else tmp=carry+(a[i]-'0')+(b[j]-'0');
10.
11.
12.
13.
14.
15.
16.}

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.}

Lets see implementation of compare function

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

--------- Ans may have leading


zeros
-

We have to remove leading zeros


00001

Lets see the function to remove


leading zeros

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.
}

Lets see the full code

1.
2.
3.
4.
5.

string subtract(string a,string b)


{
int cmp=compare(a,b);
if(cmp==0) return "0";
if(cmp==-1) swap(a,b);

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.

for(int i=a.size()-1; i>=0; i--)


{
carry=0;
for(int j=b.size()-1; j>=0; j--)
{
tmp=carry+(a[i]-'0')*(b[j]-'0');
carry=tmp/10;
tmp=tmp%10;
str+=tmp+'0';
}
if(carry) str+=carry+'0';
reverse(str.begin(),str.end());
if(i==a.size()-1) ans=str;
else ans=add(ans,str);
}

Multiplication
Step 3 :

12345
111
------------12345
123450
1234500
-------------1370295

Add one more zero with str each time


for(int k=0 ; k<a.size()-i-1 ; k++) str+='0';

Lets see the full code ...

1. string multiply(string a,string b)


2. {
3.
string ans,str;
4.
int tmp,carry;
5.
for(int i=a.size()-1; i>=0; i--)
6.
{
7.
str="";
8.
for(int k=0; k<a.size()-i-1; k++) str+='0';
9.
carry=0;
10.
for(int j=b.size()-1; j>=0; j--)
11.
{
12.
tmp=carry+(a[i]-'0')*(b[j]-'0');
13.
carry=tmp/10;
14.
tmp=tmp%10;
15.
str+=tmp+'0';
16.
}
17.
if(carry) str+=carry+'0';
18.
reverse(str.begin(),str.end());
19.
if(i==a.size()-1) ans=str;
20.
else ans=add(ans,str);
21.
}
22.
return ans;
23. }

Using Java BigInteger

About Java BigInteger

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.

public class Main


{
public static void main(String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

9.
10.
11.
12.

while (true)
{
String str1 = br.readLine();
String str2 = br.readLine();

13.
14.

BigInteger x = new BigInteger(str1);


BigInteger y = new BigInteger(str2);

15.

if(x.compareTo(BigInteger.ZERO)==0 && y.compareTo(BigInteger.ZERO)==0)


break;

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

You might also like