skip to Main Content

I was making a summation algorithm to add 2 very big numbers.

//input 
let n1 = 1234567891234556555558
let n2 = 1234567891234556555558

//processing
let a = n1.toString()
let b = n2.toString()
let c = 0  //carry
let r = ''  //final result of summation

for (let i = 1; i <= Math.max(a.length, b.length); i++) {
    if(a.length == b.length) {
        let v = parseInt(a[a.length - i]) + parseInt(b[b.length - i]) + c; c = 0
        if(v.toString().length == 1) {
            r = v + r.slice(0, r.length)
        }
        else {
            r = parseInt(v.toString()[1]) + r.slice(0, r.length)
            c = parseInt(v.toString()[0])
        }
    }
}

//output
console.log(r)

The code works for small values but returns a NaN for big values.

I tried to change how the c is declared, I thought it might be still a string but the result is still the same.

Please note the algorithm only works for numbers of the same length.

2

Answers


  1. JavaScript’s Number type can’t handle very large integers accurately because of precision limits. To work with such large numbers, you should use strings and manually handle the addition of each digit.

    Here’s a working code:

        let n1 = "1234567891234556555558";
        let n2 = "1234567891234556555558";
    
        // processing
        let a = n1.toString();
        let b = n2.toString();
        let c = 0; // carry
        let r = ''; // final result of summation
    
        // Reverse the strings to make addition easier from the least significant digit
        a = a.split('').reverse().join('');
        b = b.split('').reverse().join('');
    
        // Loop through each digit
        for (let i = 0; i < Math.max(a.length, b.length); i++) {
            let n1a = i < a.length ? parseInt(a[i]) : 0;
            let n2a = i < b.length ? parseInt(b[i]) : 0;
    
            let sum = n1a + n2a + c;
            c = Math.floor(sum / 10); // carry for next iteration
            r = (sum % 10) + r; // current digit result
        }
    
        // If there's a carry left at the end, add it to the result
        if (c > 0) {
            r = c + r;
        }
    
        console.log(`output : ${r}`); // Output the result
        alert(`output : ${r}`);
    Login or Signup to reply.
  2. — Here is my updated code after remove .toString(); function,

        let a = "1234567891234556555558";
        let b = "1234567891234556555558";
    
        // processing
        let c = 0; // carry
        let r = ''; // final result of summation
    
        // Reverse the strings to make addition easier from the least significant digit
        a = a.split('').reverse().join('');
        b = b.split('').reverse().join('');
    
        // Loop through each digit
        for (let i = 0; i < Math.max(a.length, b.length); i++) {
            let n1a = i < a.length ? parseInt(a[i]) : 0;
            let n2a = i < b.length ? parseInt(b[i]) : 0;
    
            let sum = n1a + n2a + c;
            c = Math.floor(sum / 10); // carry for next iteration
            r = (sum % 10) + r; // current digit result
        }
    
        // If there's a carry left at the end, add it to the result
        if (c > 0) {
            r = c + r;
        }
    
        console.log(`output : ${r}`); // Output the result
        alert(`output : ${r}`)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search