This content originally appeared on Level Up Coding - Medium and was authored by Parul Malhotra

Let us dissect a really popular algorithmic challenge, Calculating the Sum of Two Very Large Numbers in Javascript.
Traditionally, Javascript numbers are safe to use up to 53 bits. If you really would like to know more details, you can check my last article which talks about the concept of floating point numbers in Javascript.
Problem at hand
Create an Algorithm which contains a function that takes in any two numbers but in the form of strings, and returns the sum as a string.
This function will be used for adding very large numbers in Javascript. The need for this program arises because you cannot store more than 53 bits as a number in pure javascript.
In order to perform operations on larger numbers, we convert them into strings and then perform operations on them.
Step by Step Solution
Pseudocode for the problem goes like this:

Let’s break this down step by step —
Initialising two very large numbers
Let’s say : first = “893427328497983427893248932498034289324” and second = “234859234879342897893427893274”.
Calculating the length of both the variables
The longer number should be the first number to be passed as a parameter to the function call as larger number should be on top during addition. In case first number is smaller, I am swapping the parameters in the function call.
Convert rightmost bits to numbers and add them
Here, diff represent the different in the length of the two variables. We are using it to reference the correct i-th element and add them. Variable temp is calculated by converting the character at the i-th position and doing modulus of 10 to get the remainder. We add the two elements from first and second variable and add carry variable to it, if any.
Create a sum variable of type string
This variable, sum is going to store the result of the summation for us.
Store the sum’s right most bit in “sum” variable
While performing the summation, we shall store the rightmost bits in sum variable and leftmost bits in carry variable, just like we do it in general.
Run the loop for the entire length
We keep adding the right most bits. Store them in temp variavle. If it is greater than equal to 10, we take a modulus of 10 on it and store the result in sum. We keep the quotient in carry.
For the last iteration, add the carry to sum only
In the end, we return the sum variable which now holds the summed up value of these two digits.
Thanks for reading!
How To Add Two Very Large Numbers in Javascript? was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Parul Malhotra

Parul Malhotra | Sciencx (2021-08-30T01:09:55+00:00) How To Add Two Very Large Numbers in Javascript?. Retrieved from https://www.scien.cx/2021/08/30/how-to-add-two-very-large-numbers-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.