Last active
December 22, 2018 19:53
-
-
Save haocong/c2d9b2169d28eb15a94d to your computer and use it in GitHub Desktop.
Karatsuba Multiplication in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Karatsuba Multiplication | |
* @param {Number} x - first number | |
* @param {Number} y - second number | |
* @return {Number} Multiply of x and y | |
*/ | |
function karatsubaMulti(x, y) { | |
let n = Math.min(('' + x).length, ('' + y).length); | |
if(n == 1) | |
return x * y; | |
let tenpowhalfn = Math.pow(10, parseInt(n / 2)); | |
let tenpown = Math.pow(10, 2 * parseInt(n / 2)); | |
let a = parseInt(x / tenpowhalfn); | |
let b = x % tenpowhalfn; | |
let c = parseInt(y / tenpowhalfn); | |
let d = y % tenpowhalfn; | |
let caller = arguments.callee; | |
return tenpown * caller(a, c) + tenpowhalfn * (caller(a, d) + caller(b, c)) + caller(b, d); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IF you get the error
All you have to do is remove line 23, 'let caller = .... ' and change the return to