10
\$\begingroup\$

I got a very unusual problem of adding and multiplying very big numbers (≥ 1e+100). So I've written simple functions that would operate on string representations of numbers, both as an input and an output.

Multiplication:

function multiply(a, b) {
    if ((a | 0) == 0 || (b | 0) == 0) {
        return '0';
    }

    a = a.split('').reverse();
    b = b.split('').reverse();
    var result = [];

    for (var i = 0; a[i] >= 0; i++) {
        for (var j = 0; b[j] >= 0; j++) {
            if (!result[i + j]) {
                result[i + j] = 0;
            }

            result[i + j] += a[i] * b[j];
        }
    }

    for (var i = 0; result[i] >= 0; i++) {
        if (result[i] >= 10) {
            if (!result[i + 1]) {
                result[i + 1] = 0;
            }

            result[i + 1] += parseInt(result[i] / 10);
            result[i] %= 10;
        }
    }

    return result.reverse().join('');
}

Addition:

function add(a, b) {
    if ((a | 0) == 0 && (b | 0) == 0) {
        return '0';
    }

    a = a.split('').reverse();
    b = b.split('').reverse();
    var result = [];

    for (var i = 0; (a[i] >= 0) || (b[i] >= 0); i++) {
        var sum = (parseInt(a[i]) || 0) + (parseInt(b[i]) || 0);

        if (!result[i]) {
            result[i] = 0;
        }

        var next = ((result[i] + sum) / 10) | 0;
        result[i] = (result[i] + sum) % 10;

        if (next) {
            result[i + 1] = next;
        }
    }

    return result.reverse().join('');
}

Are there better ways of doing this job? Some additional edge cases or better loop conditions, maybe?

\$\endgroup\$
  • \$\begingroup\$ Unless this is just for fun you'd be better off looking for a bignum library instead. \$\endgroup\$ – ferada Jun 8 '15 at 11:02
  • 3
    \$\begingroup\$ Take a look at javascript-bignum; I'm quite fond of it. (I'd review your code if I did enough Javascript to understand half of it) \$\endgroup\$ – Nic Hartley Jun 8 '15 at 12:47
  • \$\begingroup\$ Your function works for small numbers but for large numbers, it is not working. e.g. multiply('1111','222222222222222222222222222222') resulted in 0. Fix it please. \$\endgroup\$ – Merin Nakarmi Mar 1 '18 at 20:09
2
\$\begingroup\$

Use the correct functions to convert to types

I think you are using the bitwise | to convert a string to a number:

if ((a | 0) == 0 || (b | 0) == 0) {
    return '0';
}

I think parseInt can show this intent more clearly:

if (parseInt(a) == 0 || parseInt(b) == 0) {
    return '0';
}
\$\endgroup\$
  • \$\begingroup\$ That would get you an Integer not any number (floating point). Not a big difference, but it would be helpful to point it out as you wouldn't use it just anywhere \$\endgroup\$ – Zorgatone Jan 10 '16 at 13:44

protected by Jamal Aug 28 '17 at 2:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.