vanless vanless - 12 days ago 6
Javascript Question

karatsuba algorithm, Maximum call stack size exceeded

i'm write own variant of karatsuba algh, but can't make it work with realy big integers

'use strict';

const karatsuba = function (x, y) {
if (x < 10 || y < 10) return x * y;

const strX = String(x);
const strY = String(y);

const n = Math.min(strX.length, strY.length);
const m = Math.ceil(n / 2);

const leftX = strX.slice(0, strX.length - m),
rightX = strX.slice(strX.length - m, strX.length),

leftY = strY.slice(0, strY.length - m),
rightY = strY.slice(strY.length - m, strY.length);

const prod1 = karatsuba(leftX, leftY),
prod2 = karatsuba(rightX, rightY),
prod3 = karatsuba(addAny(leftX, rightX), addAny(leftY, rightY));

const a = prod1 + String(Math.pow(10, 2 * m)).slice(1),
b = (prod3 - prod1 - prod2) + String(Math.pow(10, m)).slice(1),
ab = addAny(a, b);

return addAny(ab, prod2);
};

function addAny(a, b) {
const MAX_INT = 9007199254740992,
intA = parseInt(a),
intB = parseInt(b);

if ((intA + intB) < MAX_INT) return intA + intB;

return sumStrings(a + '', b + '');
}



function sumStrings(a, b) {
let res = '', c = 0;

a = a.split('');
b = b.split('');

while (a.length || b.length || c) {
c += ~~a.pop() + ~~b.pop();
res = c % 10 + res;
c = c > 9;
}

return res.replace(/^0+/, '');
}


it works fine with ints like 3957322621233333 and 5548313756335578
but when i'm try use it with big ints like 76715432964249374812219365555 and 32141964835273822784327848699719 code crash with

RangeError: Maximum call stack size exceeded

Kay Kay
Answer

You omitted the quotation marks in your call, didn't you?

> katsuba(76715432964249374812219365555,32141964835273822784327848699719)
RangeError: Maximum call stack size exceeded
    at karatsuba (repl:2:5)
    at karatsuba (repl:16:15)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)
    at karatsuba (repl:17:9)

76715432964249374812219365555 is bigger than 5**53, the MAX_INT in your code. The number literal is implicitly converted to a floating point number 7.671543296424938e+28, and your algorithm chokes on the number format.

Nevertheless, there is some error in your implementation, because the calculated result is wrong even if you use string literals:

> karatsuba('76715432964249374812219365555', '32141964835273822784327848699719')
'1045731853714033916818600413783559075'

instead of

'2465784748659709650840276767201870909665028593909797886779045'
Comments