This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Karatsuba Multiplication #7
Comments
Sounds good. I'll keep the thread open if you post it here and label it with something like |
Here is the code function karatsubaMulti(x, y) {
let n = Math.min(('' + x).length, ('' + y).length); // Implicit coercision
if(n == 1)
return x * y;
let bm = Math.pow(10, parseInt(n / 2));
let bm2 = Math.pow(10, 2 * parseInt(n / 2));
let a = parseInt(x / bm);
let b = x % bm;
let c = parseInt(y / bm);
let d = y % bm;
let caller = arguments.callee; // For recursive call, not using TCO
return bm2 * caller(a, c) + bm * (caller(a, d) + caller(b, c)) + caller(b, d);
} let a = karatsubaMulti(12345, 6789);
console.log(a); // 83810205 Works fine but exceeds the maximum call stack size when arrow functions are used instead. |
Just a heads up that https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee |
Oh ! Thanks for pointing this. I'll rewrite the code 😄 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Can provide the code for karatsuba multiplication (<=20 lines).
The text was updated successfully, but these errors were encountered: