SlideShare a Scribd company logo
1 of 2
Download to read offline
Problem 2.19. How much time is required to add or subtract two large integers of size m and n
respectively? Sketch the appropriate algorithm.
Solution
For the large integers, they can not be accomodated in the permitted range of 32 bits of standard
integer(Exact size depends on the system and environment).
So to add two very large numbers, we can keep them in form of string and then process them
from right side, one character at a time.
Below is the algorithm:
Suppose we have got two nos 125372 432
1. Take two strings s1 and s2.
2. copy 125372 in s1, and 432 in s2
3. Compute the length diff: abs(length(s1) - length(s2)). Left pad the small string with zeroes so
that size of both the strings are same. Lets assume length is n now.
4. Create a result string of (n+1) character, as the addition might produce a carry on the last.
5. Now start from the right most digit of both nos. Compute the int value of single digit in both
nos as shown below. To get the int value of a digit, we subtract '0' in from of character from the
String chacter, so that we get exact digit.
6. Now sum both these digits and put the result's unit bit in resultant string at ith position. You
might get an carry in this step, which you need to remember, when you go to process the (i-1)th
digit of the nos.
For( i=n; i >= 1; i-- ) {
// TO find out the digit at iTh pos in s1 in number format from character format
int n1 = s1.charAt(i) - '0'
int n2 = s2.charAt(i) - '0'
int carry = ((n1+n2)>=10) ? 1: 0;
result[i+1] = '0' + ((n1+n2)%10); //only put unit digit in result string, that too in form of
character
}
7. Keep doing the above steps for all the n digits. Now you may get a overall carry at the last
step, which you need to keep on result[0] pos. And your reslt string is prepared with the sum.
Similary can be done for subtraction also.

More Related Content

Similar to Problem 2.19. How much time is required to add or subtract two large.pdf

4tocontest
4tocontest4tocontest
4tocontest
berthin
 

Similar to Problem 2.19. How much time is required to add or subtract two large.pdf (20)

M269 Data Structures And Computability.docx
M269 Data Structures And Computability.docxM269 Data Structures And Computability.docx
M269 Data Structures And Computability.docx
 
Number system computer fundamental
 Number  system computer fundamental  Number  system computer fundamental
Number system computer fundamental
 
B sc cs i bo-de u-i number system
B sc cs i bo-de u-i number systemB sc cs i bo-de u-i number system
B sc cs i bo-de u-i number system
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
A New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring AlgorithmA New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring Algorithm
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
5. Summing Series.pptx
5. Summing Series.pptx5. Summing Series.pptx
5. Summing Series.pptx
 
Design of QSD Number System Addition using Delayed Addition Technique
Design of QSD Number System Addition using Delayed Addition TechniqueDesign of QSD Number System Addition using Delayed Addition Technique
Design of QSD Number System Addition using Delayed Addition Technique
 
Design of QSD Number System Addition using Delayed Addition Technique
Design of QSD Number System Addition using Delayed Addition TechniqueDesign of QSD Number System Addition using Delayed Addition Technique
Design of QSD Number System Addition using Delayed Addition Technique
 
Module 1 number systems and code1
Module 1  number systems and code1Module 1  number systems and code1
Module 1 number systems and code1
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Complex numbers
Complex numbersComplex numbers
Complex numbers
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
 
Data repersentation.
Data repersentation.Data repersentation.
Data repersentation.
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
4tocontest
4tocontest4tocontest
4tocontest
 
elementry-objects-CG give great effort on learning for exam
elementry-objects-CG give great effort on learning for examelementry-objects-CG give great effort on learning for exam
elementry-objects-CG give great effort on learning for exam
 
digital-180612132737.pdf
digital-180612132737.pdfdigital-180612132737.pdf
digital-180612132737.pdf
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 

More from JUSTSTYLISH3B2MOHALI

In a business projects What material is typically contained in a pro.pdf
In a business projects What material is typically contained in a pro.pdfIn a business projects What material is typically contained in a pro.pdf
In a business projects What material is typically contained in a pro.pdf
JUSTSTYLISH3B2MOHALI
 
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
I would appreciate help with these 4 questions. Thank You.1) Expla.pdfI would appreciate help with these 4 questions. Thank You.1) Expla.pdf
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
JUSTSTYLISH3B2MOHALI
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
Explain the data component of social media information systems (SMIS).pdf
Explain the data component of social media information systems (SMIS).pdfExplain the data component of social media information systems (SMIS).pdf
Explain the data component of social media information systems (SMIS).pdf
JUSTSTYLISH3B2MOHALI
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
JUSTSTYLISH3B2MOHALI
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
What is the theory of public debt managementSolution1. Sove.pdf
What is the theory of public debt managementSolution1. Sove.pdfWhat is the theory of public debt managementSolution1. Sove.pdf
What is the theory of public debt managementSolution1. Sove.pdf
JUSTSTYLISH3B2MOHALI
 
This is a three part question. For each part the answer requires ide.pdf
This is a three part question. For each part the answer requires ide.pdfThis is a three part question. For each part the answer requires ide.pdf
This is a three part question. For each part the answer requires ide.pdf
JUSTSTYLISH3B2MOHALI
 

More from JUSTSTYLISH3B2MOHALI (20)

In a business projects What material is typically contained in a pro.pdf
In a business projects What material is typically contained in a pro.pdfIn a business projects What material is typically contained in a pro.pdf
In a business projects What material is typically contained in a pro.pdf
 
Implement the following flowchart in java. Start Request integer k f.pdf
Implement the following flowchart in java. Start Request integer k f.pdfImplement the following flowchart in java. Start Request integer k f.pdf
Implement the following flowchart in java. Start Request integer k f.pdf
 
If no chiasma forms between homologous chromosomes, what happens Th.pdf
If no chiasma forms between homologous chromosomes, what happens  Th.pdfIf no chiasma forms between homologous chromosomes, what happens  Th.pdf
If no chiasma forms between homologous chromosomes, what happens Th.pdf
 
If two organims form a symbiotic realtionship where they share t.pdf
If two organims form a symbiotic realtionship where they share t.pdfIf two organims form a symbiotic realtionship where they share t.pdf
If two organims form a symbiotic realtionship where they share t.pdf
 
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
I would appreciate help with these 4 questions. Thank You.1) Expla.pdfI would appreciate help with these 4 questions. Thank You.1) Expla.pdf
I would appreciate help with these 4 questions. Thank You.1) Expla.pdf
 
How do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdf
How do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdfHow do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdf
How do I know when to use the sin^2+cos^2=1 identity....12 angle id.pdf
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
 
Hint List of commands to read and use use of wild card characters .pdf
Hint List of commands to read and use use of wild card characters .pdfHint List of commands to read and use use of wild card characters .pdf
Hint List of commands to read and use use of wild card characters .pdf
 
Explain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdf
Explain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdfExplain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdf
Explain in Detail DTE-DCE TransmissionSolutionFirstly the DTE-.pdf
 
Explain the data component of social media information systems (SMIS).pdf
Explain the data component of social media information systems (SMIS).pdfExplain the data component of social media information systems (SMIS).pdf
Explain the data component of social media information systems (SMIS).pdf
 
Figure CWhich structure in the cell shown in Figure C above stores.pdf
Figure CWhich structure in the cell shown in Figure C above stores.pdfFigure CWhich structure in the cell shown in Figure C above stores.pdf
Figure CWhich structure in the cell shown in Figure C above stores.pdf
 
Coral reefs. How sensitive to changes in water temperature are coral .pdf
Coral reefs. How sensitive to changes in water temperature are coral .pdfCoral reefs. How sensitive to changes in water temperature are coral .pdf
Coral reefs. How sensitive to changes in water temperature are coral .pdf
 
•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf
•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf
•0.336 moles of a weak, monoproticacid added to a final volume of 2..pdf
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Why was the knowledge of macromolecules “structure” very important i.pdf
Why was the knowledge of macromolecules “structure” very important i.pdfWhy was the knowledge of macromolecules “structure” very important i.pdf
Why was the knowledge of macromolecules “structure” very important i.pdf
 
Why did the sovereign debt problem of Greece a country that accounts.pdf
Why did the sovereign debt problem of Greece a country that accounts.pdfWhy did the sovereign debt problem of Greece a country that accounts.pdf
Why did the sovereign debt problem of Greece a country that accounts.pdf
 
Which of the following could be the most likely cause of a superi.pdf
Which of the following could be the most likely cause of a superi.pdfWhich of the following could be the most likely cause of a superi.pdf
Which of the following could be the most likely cause of a superi.pdf
 
What is the theory of public debt managementSolution1. Sove.pdf
What is the theory of public debt managementSolution1. Sove.pdfWhat is the theory of public debt managementSolution1. Sove.pdf
What is the theory of public debt managementSolution1. Sove.pdf
 
This is a three part question. For each part the answer requires ide.pdf
This is a three part question. For each part the answer requires ide.pdfThis is a three part question. For each part the answer requires ide.pdf
This is a three part question. For each part the answer requires ide.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

Problem 2.19. How much time is required to add or subtract two large.pdf

  • 1. Problem 2.19. How much time is required to add or subtract two large integers of size m and n respectively? Sketch the appropriate algorithm. Solution For the large integers, they can not be accomodated in the permitted range of 32 bits of standard integer(Exact size depends on the system and environment). So to add two very large numbers, we can keep them in form of string and then process them from right side, one character at a time. Below is the algorithm: Suppose we have got two nos 125372 432 1. Take two strings s1 and s2. 2. copy 125372 in s1, and 432 in s2 3. Compute the length diff: abs(length(s1) - length(s2)). Left pad the small string with zeroes so that size of both the strings are same. Lets assume length is n now. 4. Create a result string of (n+1) character, as the addition might produce a carry on the last. 5. Now start from the right most digit of both nos. Compute the int value of single digit in both nos as shown below. To get the int value of a digit, we subtract '0' in from of character from the String chacter, so that we get exact digit. 6. Now sum both these digits and put the result's unit bit in resultant string at ith position. You might get an carry in this step, which you need to remember, when you go to process the (i-1)th digit of the nos. For( i=n; i >= 1; i-- ) { // TO find out the digit at iTh pos in s1 in number format from character format int n1 = s1.charAt(i) - '0' int n2 = s2.charAt(i) - '0' int carry = ((n1+n2)>=10) ? 1: 0; result[i+1] = '0' + ((n1+n2)%10); //only put unit digit in result string, that too in form of character } 7. Keep doing the above steps for all the n digits. Now you may get a overall carry at the last step, which you need to keep on result[0] pos. And your reslt string is prepared with the sum.
  • 2. Similary can be done for subtraction also.